1

I have a page which has multiple file upload inputs whcih have ID's and Names relevant to the document type being uploaded which look like:

<input type="file" name="postedFile_37" id="37">
<input type="file" name="postedFile_23" id="23">

In my controller, how can I identify the name or id of the upload so I can assign a document to the type being uploaded in the DB?

I can see for example that if I do

Request.Files[i]

I can see the name of the index but I can't get the value to save out. How can I get either the name or ID from the posted file upload?

valverij
  • 4,461
  • 1
  • 19
  • 33
Codesight
  • 305
  • 2
  • 11

2 Answers2

2

Try adding hidden fields next to each of the files so you have two arrays - first is file itself and second is id.

<input type="hidden" name="fileId" value="37" />
<input type="file" name="file" />
<input type="hidden" name="fileId" value="38" />
<input type="file" name="file" />

.

public ActionResult Test (string[] fileId, List<HttpPostedFileBase> file)
{
    int i = 0;
    foreach (var f in file)
    {
        var id = fileId[i]; // this is your file id, f is file
        i++;
    }
}
Stan
  • 22,856
  • 45
  • 148
  • 231
  • I did think about doing this but wondered if there was a way of getting the ID off the upload. Thanks. – Codesight Jun 07 '13 at 14:15
  • 1
    AFAIK it's not possible by HTML standards. More info here: http://stackoverflow.com/questions/7470268/html-input-name-vs-id – Stan Jun 07 '13 at 14:40
0

If you use the ASP.NET FileUpload control instead of the plain HTML control, you can access them individually (by ID) in the PostBack.

Alexander
  • 2,449
  • 1
  • 12
  • 17