Cannot upload files using ASP.NET MVC2

I installed enctype for: multipart / form-data, but whenever I submit this form, Request.ContentType: application / x-www-form-urlencoded and the contents of the download cannot be obtained from the .files request.

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(); } 

The variable "fc" is populated in PostBack, and I can access the checkbox value and get the file name to upload.

Why will my enctype be ignored?

I tried to manually put the form tag in the view with attributes in different positions, but that didn't matter.

The only thing I can think of is that this import form is nested in the MasterPage form, but it does not look like this should be a problem. Plus I have this form, properly enclosed.

Any suggestions?

+4
source share
1 answer

I believe that there are two problems here:

The only thing I can think of is that this import form is nested in the MasterPage form, but it does not look like this should be a problem. Plus I have this form, properly enclosed.

This is probably a big part of the problem - two things bother me:

  • Nested forms are not legal (X) HTML - the browser is quite legally allowed to ignore the declaration of the second form and close the form in the first tag of the form to which it comes - so there may very well be the problem you see.
  • Since we are dealing with ASP.NET MVC, there is no reason to have one large closing form on the main page, which just gets in the way (as we see here).

I recommend removing the form from the main page and just adding it when you need it, in a real form. It can also solve the problem you see.

Secondly, you need to add a parameter based on HttpPostedFileBase to your action:

 public ActionResult Import(FormCollection fc, HttpPostedFileWrapper FileUpload) { string chkDelete = fc["DeleteExisting"]; if (null != FileUpload && 0 < FileUpload.ContentLength) { // We have an upload. string filename = FileUpload.FileName; if (!chkDelete.Equals("false")) { //TODO: delete existing records, if specified } // Stream file in from FileUpload.InputStream eg: var bytesOriginal = new byte[FileUpload.ContentLength]; FileUpload.InputStream.Read(bytesOriginal, 0, FileUpload.ContentLength); //Read from the byte array as you would any normal file. } return View(); } 

I just tried this in a simple view (no master page, no nested forms), and it behaved exactly as I expected. Without HttpPostedFileWrapper, the FormCollection contained only a flag.

+4
source

Source: https://habr.com/ru/post/1313791/


All Articles