Reputation: 1421
I am looking for a way to perfectly manage the <input type="file" multiple="multiple">
tag of HTML 5 in my asp.net 3.5 project.
I have done it before with a single control on page, but what if we have multiple upload controls on the same page. Please see my code:
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(" ", "_"));
}
}
}
}
}
My asp page is something like this:
//----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" />
So, exactly my problem is that when page loads "fupMailingList" has taken a file, and then when I want to use my multiple upload control "fupAttachment", I am unable to check if it has any files or not, as hfc
checks for all upload controls and it gets file in one of them. So, please tell me a way to check only "fupAttachment" control and then do my work correctly.
Upvotes: 0
Views: 1567
Reputation: 5545
Just check the HasFile property.
if(fupMailingList.HasFile){
//Do something
}
Upvotes: 0
Reputation: 17724
Rather than iterating over all the files in the request, you should check on a per input basis.
var uploadedFiles = Request.Files.GetMultiple("fupAttachment");
if(uploadedFiles.Count > 0)
{
//
}
Upvotes: 1