12

I'm trying to upload a file using Ajax.BeginForm(), but it's not working out.

My view contains:

@using (Ajax.BeginForm("UploadFile", null, new AjaxOptions { HttpMethod="POST",     UpdateTargetId = "result" }, new { enctype = "multipart/form-data" }))
{
   <label id="lblUploadNewFile" for="fileUploadControl">Upload New File&lt;/label>
   <input type="file" name="fileToUpload" id="fileUploadControl"/>
   <input id="btnFileUpload" type="submit" value="Upload" />
   <span id="result" />
}

and the corresponding Controller is:

[HttpPost]
public string UploadFile(FormCollection formData)
{
   HttpPostedFileBase file=null;

   try
   {
      file = Request.Files[0];
   }
   catch { }

   if ( file!=null &amp;&amp; file.ContentLength &gt; 0)
   {
      file.SaveAs(string.Concat(
            AppDomain.CurrentDomain.BaseDirectory,
            Path.GetFileName(file.FileName)));

      return &quot;Successfully Uploaded&quot;;
   }
   else
   {
      return &quot;Upload Failed, please try again.&quot;;
   }
}

The problem is that it's uploading the file, but no longer doing any asynchronous posts when I remove jquery.unobtrusive-ajax.js. Instead, it does a full post-back.

When I add jquery.unobtrusive-ajax.js in my view, it's doing it asynchronously, but it is not sending an upload file in the form data. No file is being sent to the server in Request.Files[].

1
  • 1
    It's not possible with Ajax for obvious reasons: you could build a page hat uploads any accessible file on the visitors filesystem to the server without telling him. Commented Jun 11, 2013 at 6:57

4 Answers 4

17

You cannot upload files using AJAX. This is not supported. If you want to do that you could either use some file upload plugin such as Uploadify or Blueimp File Upload or use the HTML 5 File API if the client browser supports it.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks Darin for your reply, I'll try implementing Blueimp as Uploadify requires flash.
I'm using jQuery-asyncUpload shared by steven: blog.stevensanderson.com/2008/11/24/…
@Darin Dimitrov, I don't understand your answer. As per this other StackOverflow question, file uploading is actually supported and the way to do it is given in that other answer. Can you please clear what you say up?
@Darin Dimitrov Yes, could you please clarify us a little bit more regarding to this issue?
10

You can do this without additional libraries.

I came across this little hack, which resolves it nicely

window.addEventListener("submit", function (e) {
    var form = e.target;
    if (form.getAttribute("enctype") === "multipart/form-data") {
        if (form.dataset.ajax) {
            e.preventDefault();
            e.stopImmediatePropagation();
            var xhr = new XMLHttpRequest();
            xhr.open(form.method, form.action);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    if (form.dataset.ajaxUpdate) {
                        var updateTarget = document.querySelector(form.dataset.ajaxUpdate);
                        if (updateTarget) {
                            updateTarget.innerHTML = xhr.responseText;
                        } 
                    }
                }
            };
            xhr.send(new FormData(form));
        }
    }
}, true);

Found http://www.acnenomor.com/1762557p1/c-mvc3-ajaxbeginform-to-upload-file-not-working

3 Comments

http://www.acnenomor.com/1762557p1/c-mvc3-ajaxbeginform-to-upload-file-not-working is no more; it was a scraper site copying Stack Overflow, you really found this post.
whoever you are I'll find you and love you!
But how to implement the call back for the form ??
1

very simple solution:

  @using (Ajax.BeginForm("action", "controller", new AjaxOptions
        {
            HttpMethod = "post",
            InsertionMode = InsertionMode.Replace,
            OnBegin = "startLoader",
            OnSuccess = "Update_Record",
            UpdateTargetId = "Succ_Msg"
        },new { enctype = "multipart/form-data" }))

notice the new { enctype = "multipart/form-data" } which tells the razor page to add the enctype that can pass HttpPostedFileBase

good luck:).

2 Comments

hey @tzuk, that's exactly what I've posted in question!
Does this technique require a particular version of .net framework? When I add , new { enctype = "multipart/form-data" } to an existing Ajax.BeginForm that has AjaxOptions, then the action loses resolution. As if it's illegal. Using Rider, if it makes a difference, and .net framework v4.7.2.
0

You can do this to get this work: get jquery.form library from nuget and import it to your cshtml file.You should create your form with @Html.BeginForm. then bottom of your form write this script to ajax-ify your form:

$(function(){
  $('#formId').ajaxForm({
    complete:function(xhr){...},
    success:function(){...},
    error: function(){...}
  });
});

With these steps you can pass a file to controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.