簡體   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