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

时间:2021-05-03 08:03:20

标签: c# asp.net

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

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

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

index.html
<form method="post">
<input type="submit">
</form>
{% if allowed == "yes" %}
<!-- your table code -->
{% endif %}


and in views.py
if request.method == "POST":
    return render(request, 'index.html',{'allowed':'yes'})

else:
    return render(request,'index.html',{'allowed':'no'})

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

如果您不希望从该函数中删除它,您可以将 EndResponse 设置为 false 以防止它终止执行。

相关问题