ASP.NET File Upload Progress - Knowledge Base

« Back to Knowledge Base list

Uploaded files identifying.

Q:

When putting multiple upload <input> on a single page, we needthe ability to identify which file is associated with a certain uploadbutton.

For example:

<input type="file" id="myFile1" />
<div id="myFile1Files">
</div>
...
<input type="file" id="myFile2" />
<div id="myFile2Files">
</div>
...
<input type="file" id="myFile3" />
<div id="myFile3Files">
</div>


After the postback we need to be able to see that myFile1 triggered file a,  while myFile2 triggered file b.. etc..
As it is now all files are uploaded together and we can't tell where they came from.
Is this possible any way?

A:

Use InputName property of OboutPostedFile class.

Example:

void Page_Load(object sender, EventArgs e)
{
   if (Page.IsPostBack)
   {
      OboutFileCollection files = uploadProgress.Files;
      uploadedFiles.Text = "";

      for (int i = 0; i < files.Count; i++)
      {
         OboutPostedFile file = files[i];
         string fileName = Path.GetFileName(file.FileName);

         file.SaveAs(MapPath("/FileUploadProgress/Uploaded/"+fileName));

         uploadedFiles.Text += " FileName: "+fileName;
         uploadedFiles.Text += " ContentLength: "+file.ContentLength.ToString();
         uploadedFiles.Text += " ContentType: "+file.ContentType;
         uploadedFiles.Text += " InputName: \""+file.InputName;
         uploadedFiles.Text += "<br/>";
   }

   if (uploadedFiles.Text.Length == 0)
      uploadedFiles.Text = "no files";
  }
}
...
<fup:FileUploadProgress
     ShowUploadedFiles="true"
     runat="server"
     ID="uploadProgress"
     InnerFiles="true"
/>
<
asp:Label runat="server" id="uploadedFiles" Text="" />