简体   繁体   English

ASP.NET C# 两个 Single FileUpload 一次只从一个控件上传文件

[英]ASP.NET C# Two Single FileUpload only uploads file from one control at a time

I have two separate asp:FileUpload controls under a single asp form like this:我在单个 asp 表单下有两个单独的 asp:FileUpload 控件,如下所示:

https://i.stack.imgur.com/KJBU9.png https://i.stack.imgur.com/KJBU9.png

I have written two different functions to save two different files onto a local directory我编写了两个不同的函数来将两个不同的文件保存到本地目录中

protected void profileImageUpload()
    {

            if (profile_image_input.HasFile)
            {
            string imagePath = "/Images/" + Session["name"]+"_profile_image"+ System.IO.Path.GetExtension(profile_image_input.FileName);
            profile_image_input.SaveAs(Server.MapPath(imagePath));

            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update uni_login set profile_image_path =" + "'" + imagePath + "'" + "where name =" + "'" + Session["name"] + "';";
            cmd.ExecuteNonQuery();
            con.Close();

            Response.Redirect(Request.RawUrl);
            }
    }

    protected void campusImageUpload()
    {

        if (campus_image_input.HasFile)
        {
            string imagePath = "/Images/" + Session["name"] + "_campus_image" + System.IO.Path.GetExtension(campus_image_input.FileName);
            campus_image_input.SaveAs(Server.MapPath(imagePath));

            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update uni_login set campus_image_path =" + "'" + imagePath + "'" + "where name =" + "'" + Session["name"] + "';";
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

and when I click the "Update Profile" button as seen on the image above, both of the functions get called当我点击上图所示的“更新配置文件”按钮时,这两个函数都会被调用

protected void updateProfileClick(object sender, EventArgs e)
    {
        profileImageUpload();
        campusImageUpload();
        Response.Redirect(Request.RawUrl);
    }

and onclick:和 onclick:

<asp:Button ID="update_profile_button" Text="Update Profile" OnClick="updateProfileClick" runat="server" CssClass="btn btn-primary"/>

but the problem here is that when the page is loaded and I try to upload two files, that is when i populate both the fileuploads with a file, only the file from first fileupload control gets saved to local directory but when i leave the first fileupload control blank and upload a file on the second fileupload control it gets saved.但这里的问题是,当页面被加载并且我尝试上传两个文件时,即当我用一个文件填充两个文件上传时,只有来自第一个文件上传控件的文件被保存到本地目录但是当我离开第一个文件上传控制空白并在第二个文件上传控件上上传文件,它被保存。 so the main problem here is that only one file from two of these fileuploads actually gets saved onto the local directory.所以这里的主要问题是,其中两个文件上传中只有一个文件实际上保存到本地目录中。 This seems to be a unique problem I couldn't find solution to this anywhere so any help would be appreciated.这似乎是一个独特的问题,我无法在任何地方找到解决方案,因此我们将不胜感激。

Because you already Response.Redirect(Request.RawUrl) in your first function call.因为您已经Response.Redirect(Request.RawUrl)在您的第一个 function 调用中。 Remove it should solved your problem.删除它应该可以解决您的问题。

Response.Redirect(string url, bool endResponse) by default the EndResponse parameter is set to true, which will terminate the execution of the current page and the code written after it will not be visit. Response.Redirect(string url, bool endResponse) EndResponse参数默认设置为true,会终止当前页面的执行,之后写的代码不会被访问。

You may set EndResponse to false to prevent it from terminating the execution if you do not wish to remove it from that function.如果您不想从 function 中删除它,您可以将 EndResponse 设置为 false 以防止它终止执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM