Solved

MVC 5 Ajax BeginForm File Upload No File?

Posted on 2014-07-24
4
5,841 Views
Last Modified: 2014-07-25
Q. What am I missing that stops this Ajax.BeginForm from uploading a file to the controller action?

1. I always receive a null on the Action 'files'...
2. Unsure if Jquery v. 1.11.1 is an issue

[View] 
  <div id="ProfileFileUpload">
      @{ Html.RenderPartial("_FileUpload"); }
  </div>

[PartialView]
@Scripts.Render("~/bundles/jquery")
@using (Ajax.BeginForm("Upload", "MyAccount", null, new AjaxOptions {  HttpMethod = "POST", UpdateTargetId = "ProfileFileUpload" }, new { enctype = "multipart/form-data"}))
{   
    <input type="file" id="file" name="files" />
    <br>
    <input type="submit" id="submit" value="Upload Image" />                            
}


  [Action]
  [HttpPost]
  public ActionResult Upload(IEnumerable<HttpPostedFileBase> files) 
  {
       foreach (var file in files)
       {
            if (file != null && file.ContentLength > 0)
            {
             }
        }
     return PartialView("_FileUpload"); 
  }

[View Source]
<script src="/Scripts/jquery-1.11.1.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

<form action="/MyAccount/Upload" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#FileUpload" enctype="multipart/form-data" id="form0" method="post">    

    <input type="file" id="file" name="files" />
    <br>
    <input type="submit" id="submit" value="Upload Image" />                            
</form>

Open in new window

0
Comment
Question by:WorknHardr
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 475 total points
ID: 40217723
I tried running your code and it works fine i.e. I can see value in the file.
I tested in IE10 and FF.

Not sure what could be causing that...
0
 

Author Comment

by:WorknHardr
ID: 40219251
Q. What Jquery version are you using?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 40219875
I tried 1.10.2 and 1.11.1. You can try switching the jquery version on your file and see if that makes any difference for you.
0
 

Author Closing Comment

by:WorknHardr
ID: 40219884
Thanks for testing the code. I really appreciate it, thx...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question