Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC 5 Ajax BeginForm File Upload No File?

Posted on 2014-07-24
4
Medium Priority
?
6,895 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 1900 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

886 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