Solved

MVC 5 Ajax BeginForm File Upload No File?

Posted on 2014-07-24
4
5,302 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
Comment Utility
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
Comment Utility
Q. What Jquery version are you using?
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
Comment Utility
Thanks for testing the code. I really appreciate it, thx...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now