views:

25

answers:

1

I am trying to upload a couple of files. The upload itself works well for a single upload but I can't figure out how to access the element name to make sure each upload is assigned to the correct field. HttpPostedFileBase doesn't seem to contain that type of info anymore.

public ActionResult Edit(int id, FormCollection collection) {
    Report report = re.GetReport(id);
    var fileNames = new List<string>();

    foreach (string file in Request.Files) {
        var postedFile = Request.Files[file] as HttpPostedFileBase;
        if (postedFile.ContentLength == 0)
            continue;
        fileNames.Add(UploadFile(basedir, postedFile));
    }

    // Rather than guessing which is which I'd like to get the field name or id.
    report.Image = fileNames[0];
    report.File = fileNames[1];

    UpdateModel(report, "report");
    rep.Save();

In the view I have

<%: Html.LabelFor(model => model.report.Image)%>
<input id="report_Image" type="file" name="Image" />  

<%: Html.LabelFor(model => model.report.File)%>
<input id="report_Image" type="file" name="File" />   

Thanks, Duffy

A: 

Phil Haack blogged about this recently, maybe his example will help.

You could also look at the extension of the file to determine which file it is.

Can you use Request.Files["File"] and Request.Files["Image"]?

Simon Hazelton
Phil's post actually demonstrates the problem. He will save one or two files but won't be able to tell which one came from which box. Looking at the extensions works in my simplified example but fails when both uploads are of the same type. (e.g. one image for the header and one for the footer.
duffy

related questions