简体   繁体   中英

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:

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:

<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. 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.

You may set EndResponse to false to prevent it from terminating the execution if you do not wish to remove it from that function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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