[英]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.