簡體   English   中英

在ASP.NET中使用HTML5文件(多個)控件

[英]Using HTML5 file (Multiple) control in asp.net

我正在尋找一種在asp.net 3.5項目中完美管理HTML 5的<input type="file" multiple="multiple">標記的方法。 之前,我在頁面上只有一個控件,但是如果同一頁面上有多個上傳控件,該怎么辦。 請查看我的代碼:

protected void btnSave_Click(object sender, EventArgs e)
{
//---------Need to check if my upload control has files: Please suggest a perfect way 
if (fupAttachment.PostedFile != null || fupAttachment.PostedFile.FileName != "" || fupAttachment.PostedFile.ContentLength>0)//here is a problem, as it does not checks for a blank file upload control
HttpFileCollection hfc = Request.Files;
            string strDirectory = Server.MapPath("~/") + "Mailer/" + hidCampID.Value;
            if (hfc.Count>0)
            {
                if (!System.IO.Directory.Exists(strDirectory))
                {
                    System.IO.Directory.CreateDirectory(strDirectory);
                }
                if (System.IO.Directory.Exists(strDirectory))
                {
                    for (int i = 0; i < hfc.Count - 1; i++)
                    {
                        hfc[i].SaveAs(strDirectory + "/" + hfc[i].FileName.Replace(" ", "_"));
                    }
                }
            }
     }
}

我的asp頁面是這樣的:

//----this control is from which I want to multiple upload files
<input type="file" multiple="multiple" runat="server" id="fupAttachment" />

// Another upload control is there which takes input when page loads
<asp:FileUpload ID="fupMailingList" runat="server" />

因此,我的問題恰恰是當頁面加載“ fupMailingList”時獲取了一個文件,然后當我要使用多個上傳控件“ fupAttachment”時,由於hfc檢查是否有文件,我無法檢查它是否具有任何文件。所有上載控件,它都會在其中一個控件中獲取文件。 因此,請告訴我一種僅檢查“ fupAttachment”控件,然后正確執行工作的方法。

您應該在每個輸入的基礎上進行檢查,而不是遍歷請求中的所有文件。

var uploadedFiles = Request.Files.GetMultiple("fupAttachment");
if(uploadedFiles.Count > 0)
{
  //
}

只需檢查HasFile屬性。

if(fupMailingList.HasFile){
//Do something
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM