I have set the enctype to: multipart/form-data yet whenever I submit this form, the Request.ContentType is: application/x-www-form-urlencoded and the contents of the upload can’t be retrieved from Request.Files.
Here is my View:
<% using (Html.BeginForm("Import", "Content", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<p>
<%= Html.CheckBox("DeleteExisting")%> Delete Existing Records?
</p>
<p>
<input type="file" name="FileUpload" id="FileUpload" /> Select a dump file.
</p>
<p>
<input type="submit" value="Import Now" />
</p>
<% } %>
Here is my Action:
[HttpPost]
public ActionResult Import(FormCollection fc)
{
string chkDelete = fc["DeleteExisting"];
//string filename = fc["FileUpload"];
if (!chkDelete.Equals("false"))
{
//TODO: delete existing records, if specified
}
var inputFile = Request.Files["FileUpload"];
return View();
}
In the PostBack, the “fc” variable is filled and I can access the checkbox’s value and can get the filename of the upload.
Why would my enctype be ignored?
I have tried manually putting the form tag in the view with the attributes in different positions, but that made no difference.
The only thing I can think is that this Import form is nested within the MasterPage’s form, but that doesn’t seem like it should be a problem. Plus I have this form enclosed properly.
Any suggestions?
I believe there are two issues at play here:
This is probably most of the problem – there are two things that worry me:
I recommend that you remove the form from the master page and just add them in when you need them around an actual form. That may well solve the problem you’re seeing as well.
Secondly, you’ll need to add a parameter based on the
HttpPostedFileBaseto your action:I’ve just tried this in a simple view (no master page, no nested forms) and it behaved exactly as I’d expect – without the HttpPostedFileWrapper the FormCollection only contained the checkbox.