【问题标题】:Using HTML5 file (Multiple) control in asp.net在 asp.net 中使用 HTML5 文件(多个)控件
【发布时间】:2013-01-28 17:32:59
【问题描述】:

我正在寻找一种方法来完美管理我的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”控件的方法,然后正确地完成我的工作。

【问题讨论】:

    标签: c# asp.net html file-upload


    【解决方案1】:

    您应该逐个输入检查,而不是遍历请求中的所有文件。

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

    【讨论】:

      【解决方案2】:

      只需检查 HasFile 属性。

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

      【讨论】:

        猜你喜欢
        • 2016-08-10
        • 2012-12-30
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多