Solved

MVC 5 Ajax BeginForm File Upload No File?

Posted on 2014-07-24
4
6,285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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