2

I've this HTML:

<% using (Html.BeginForm("Upload", "BulkUpload", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <%: Html.TextBoxFor(model => model.UploadFile, new { @type = "file" }) %>
    <%: Html.ValidationMessageFor(model => model.UploadFile, "*") %>
    <%= Html.Button("submit", "Upload", null) %>
<% } %>

And this Model:

public class BulkUploadVM
{
    [DisplayName("File to import")]
    public HttpPostedFileBase UploadFile { get; set; }
}

And this Controller:

[HttpPost]
public ActionResult Upload(BulkUploadVM model)
{
    if (ModelState.IsValid)
    {
        var stream = model.UploadFile.InputStream;

        // Process stream ...
    }

    return View("Index", model);
}

The upload file is correctly posted to the Controller, but when the Controller returns the View with the model (which has a valid UploadFile), the filepath is empty on the page ?
Is this default behavior ? Or Am I missing something ?

5
  • What do you expect the filepath to be? Commented Dec 12, 2011 at 11:46
  • The same file path I entered / choose using the 'Browse' button. Commented Dec 12, 2011 at 12:58
  • This value is not available to the server, so it can't be set on a repost. Commented Dec 12, 2011 at 13:00
  • @Oded : model.UploadFile.FileName See msdn.microsoft.com/en-us/library/… Commented Dec 12, 2011 at 13:16
  • 1
    Most browsers do not provide this information for security reasons. Commented Dec 12, 2011 at 13:43

2 Answers 2

1

As already described by Oded, it's not possible to set the value from the file upload input element because of security reasons.

See RFC 1867 and this link.
The file input type creates a field through which users can upload files from their local computer or network. The VALUE attribute specifies the name of the initial file, but it is typically ignored by browsers as a security precaution.

Sign up to request clarification or add additional context in comments.

Comments

0
<% using (Html.BeginForm("Upload", "Home", new { BulkUploadVM = Model }, 
               FormMethod.Post,   new { enctype = "multipart/form-data" })) { %>

    <%= Html.TextBoxFor(model => model.UploadFile, new { @type = "file" }) %>
    <%= Html.ValidationMessageFor(model => model.UploadFile, "*") %>

    <input type="submit" value="Finish" />

    <% } %>

Set the routeValues object

new { BulkUploadVM = Model }

Comments

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.