0

I have a form with a file input that works fine with HTML form. When I submit the form, I can grab my value in the file input; but when AJAX is used, the file input field is always null.

THIS WORKS

@using (Html.BeginForm("ProcessSubmit", "Upload",
                     FormMethod.Post, new { id = "uploadForm", enctype =   "multipart/form-data" })) { 
    @(Html.Telerik().Upload().Name("attachments"))
    <p class="note">
    Maximum combined file size: 10 MB
   </p>
   <div style="margin: 20px 0 0 0;">
       <input type="submit" value="Send" class="t-button" />
       <input type="reset" value="Reset" class="t-button" />
   </div>
}    

and the controller: 
[HttpPost]
    public ActionResult ProcessSubmit(IEnumerable<HttpPostedFileBase> attachments)
    {
        if (attachments != null)
        {
            return Content("is not null");
        }
        return Content("null");
    }

But this does not work

@using (Ajax.BeginForm("ProcessSubmit", "Upload",
                     new AjaxOptions { UpdateTargetId = "mydiv" })) { 
     <div id="mydiv"></div>
    @(Html.Telerik().Upload().Name("attachments"))
    <p class="note">
    Maximum combined file size: 10 MB
   </p>
   <div style="margin: 20px 0 0 0;">
       <input type="submit" value="Send" class="t-button" />
       <input type="reset" value="Reset" class="t-button" />
   </div>
}  

In the former, the controller always returns "is not null" while in the latter it always returns null. :(

What is up with this please?

Kevin Babcock
  • 9,997
  • 19
  • 68
  • 88
jpo
  • 3,749
  • 18
  • 53
  • 102
  • 1
    This isn't a direct answer to your question, and I don't know your requirements but might I recommend taking a look at [FineUpload](http://fineuploader.com/)? I'm not affiliated with them in any regard but I have recently switched to using it in an MVC app that used to use Telerik's MVC Extensions and it was stupid simple to get working. – Nick Albrecht Feb 27 '13 at 21:33
  • I'm not sure what the Telerik file upload helper is using, but it's not possible to do an ajax file upload. To work around this most people use an iframe to mimic an asynchronous upload. http://stackoverflow.com/questions/543926/is-it-possible-to-use-ajax-to-do-file-upload – ptfaulkner Feb 27 '13 at 22:31
  • @ptfaulkner This is completely incorrect. Any browser that supports the File API can upload files via XHR. – Ray Nicholus May 14 '13 at 03:46
  • @RayNicholus, ah cool. Can you link me to any tutorial's? – ptfaulkner May 14 '13 at 12:51
  • @ptfaulkner https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications – Ray Nicholus May 14 '13 at 14:11
  • @RayNicholus, ok it's HTML5. So that leaves most IE browsers out of the mix, but still cool. I didn't realize they added that in HTML5. I guess since most of the time I'm still supporting at least back to IE8 I hadn't tried anything like this. Thanks for the info. – ptfaulkner May 14 '13 at 15:10
  • @ptfaulkner You're welcome. IE10 supports this, but I doubt that is widely used yet. For non-File API browsers, as you said, the iframe "trick" is needed. – Ray Nicholus May 14 '13 at 15:22

0 Answers0