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?

2
  • c# is a little broad. Web Forms? MVC? Something else? Commented Jun 7, 2013 at 13:55
  • I would guess at MVC, "In my controller" ^_^ Commented Jun 7, 2013 at 13:58

2 Answers 2

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++;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I did think about doing this but wondered if there was a way of getting the ID off the upload. Thanks.
AFAIK it's not possible by HTML standards. More info here: stackoverflow.com/questions/7470268/html-input-name-vs-id
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.

1 Comment

I'm using MVC Razor I don't really want to mix webforms and mvc If I can help it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.