繁体   English   中英

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

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

我在单个 asp 表单下有两个单独的 asp:FileUpload 控件,如下所示:

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

我编写了两个不同的函数来将两个不同的文件保存到本地目录中

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();
        }
    }

当我点击上图所示的“更新配置文件”按钮时,这两个函数都会被调用

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

和 onclick:

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

但这里的问题是,当页面被加载并且我尝试上传两个文件时,即当我用一个文件填充两个文件上传时,只有来自第一个文件上传控件的文件被保存到本地目录但是当我离开第一个文件上传控制空白并在第二个文件上传控件上上传文件,它被保存。 所以这里的主要问题是,其中两个文件上传中只有一个文件实际上保存到本地目录中。 这似乎是一个独特的问题,我无法在任何地方找到解决方案,因此我们将不胜感激。

因为您已经Response.Redirect(Request.RawUrl)在您的第一个 function 调用中。 删除它应该可以解决您的问题。

Response.Redirect(string url, bool endResponse) EndResponse参数默认设置为true,会终止当前页面的执行,之后写的代码不会被访问。

如果您不想从 function 中删除它,您可以将 EndResponse 设置为 false 以防止它终止执行。

暂无
暂无

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

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