FileUpload在页面刷新时上传不需要的文件 [英] FileUpload uploads undesired files on page refresh

查看:151
本文介绍了FileUpload在页面刷新时上传不需要的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net C#FIleUpload。我面临一个问题,那就是当我上传图片并将其存储在指定的文件夹中时,刷新页面后,图片再次被上传的次数与刷新页面的次数相同。我尝试启用和禁用ViewState选项,但是仍然存在相同的问题。我对功能进行了编码,当图片上传时,它将立即获得唯一的名称,因此图片不会被覆盖。谁能解释如何控制此行为,以便仅在指定的上传按钮上上传图片,而不刷新页面。
以下是我正在使用的主要代码:

I am using asp.net C# FIleUpload. I am facing a problem, that is when i upload a picture and it is stored in the specified folder, upon refreshing the page the picture again gets uploaded as many times as page is refreshed. I tried enabling and disabling ViewState option but the same problem persists. I have coded my functionality in way when a picture is uploaded it will immediately get a unique name, so pictures are not overwritten. Can anybody explain how to control this behavior, so that pictures would be uploaded only on the specified upload button and not by refreshing the page. Below is the main code i am using:

protected void btnUpload_Click(object sender, EventArgs e)
{

 if ((Session["Img1"] != null) && (Session["Img2"] != null) && (Session["Img3"] != null) && (Session["Img4"] != null))
    {
        lblUploadMsg.Text = "You cannot upload more than 4 pictures";
        return;
    }
    if (FileUpload1.HasFile)
    {
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
        if (fileExtension.ToLower() == ".jpg")
        {
            int fileSize = FileUpload1.PostedFile.ContentLength;

            if (FileUpload1.PostedFile.ContentLength < 2097152)
            {

                //FileUpload1.SaveAs(Server.MapPath("~/Temp/" + FileUpload1.FileName));
                //Response.Write("Successfully Done");

                string sp = Server.MapPath("~/ItemPictures/");
                String fn = Guid.NewGuid().ToString() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
                if (sp.EndsWith("\\") == false)
                    sp += "\\";
                sp += fn;
                FileUpload1.PostedFile.SaveAs(sp);
                lblUploadMsg.ForeColor = System.Drawing.Color.Green;
                lblUploadMsg.Text = "Picture Uploaded succefully. You can upload upto 4 pictures";
                Aziz.InnerHtml += "Image saved\n";

                if (Session["Img1"] == null)
                {
                    Session["Img1"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img2"] == null)
                {
                    Session["Img2"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img3"] == null)
                {
                    Session["Img3"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img4"] == null)
                {
                    Session["Img4"] = "~/ItemPictures/" + fn;
                }
            }
            else
            {
                lblUploadMsg.Text = "Maximum 2MB files are allowed";
            }
        }
        else
        {
            lblUploadMsg.Text = "Only JPG files are allowed";
        }
    }
    else
    {
        lblUploadMsg.Text = "No File was Selected";
    }
    ShowAvailblImgs();
  }

  private void ShowAvailblImgs()
   {
    if (Session["Img1"] != null)
    {
        Image1.ImageUrl = (string)Session["img1"];
        Image1.Width = 130;
        Image1.Height = 130;
        Image1.Visible = true;
    }
    else
        Image1.Visible = false;
    if (Session["Img2"] != null)
    {
        Image2.ImageUrl = (string)Session["img2"];
        Image2.Width = 130;
        Image2.Height = 130;
        Image2.Visible = true;
    }
    else
        Image2.Visible = false;
    if (Session["Img3"] != null)
    {
        Image3.ImageUrl = (string)Session["img3"];
        Image3.Width = 130;
        Image3.Height = 130;
        Image3.Visible = true;
    }
    else
        Image3.Visible = false;
    if (Session["Img4"] != null)
    {
        Image4.ImageUrl = (string)Session["img4"];
        Image4.Width = 130;
        Image4.Height = 130;
        Image4.Visible = true;
    }
    else
        Image4.Visible = false;
}


推荐答案

您可能希望跟踪该请求是由于页面刷新。请查看以下链接以获取有关操作方法的想法:
页面刷新导致ASP.NET应用程序中的POST重复

You may want to track if the request is because of the page refresh. Please look at the following link for ideas on how to do that: Page Refresh Causes Duplicate POST in ASP.NET Applications

这篇关于FileUpload在页面刷新时上传不需要的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆