2

I have one file input and i want to upload selected file with jquery ajax.

My input like this

<form id="formWithFiles">    
  <input type="file" name="file">
</form>

My upload jquery code

$("input[name='file']").on('change',function(){
  $.ajax({
    url: 'du.asp', 
    type: 'POST',
    contentType:'multipart/form-data',
    data: new FormData($('#formWithFiles')[0]),
    processData: false, 
    success:function(data){
      console.log(data);
    }
  });
});

My Upload Classic Asp Code - du.asp

Set Upload = Server.CreateObject("Persits.Upload")
Upload.CodePage = 65001
Upload.OverwriteFiles = False
Temp = Server.MapPath("content/temp")&"/"
Upload.Save(Temp)

Problem is here; I am getting a

500 Internal Server Error

error with jquery ajax. But if i use form submit method then file uploading with du.asp.

Detailed Error (Just using ajax)

Boundary not found in Content-Type. Make sure you have included the attribute ENCTYPE="multipart/form-data" in your form.

Emre Erol
  • 470
  • 1
  • 7
  • 25

1 Answers1

2

I did it with this code;

var formData = new FormData($("#formWithFiles")[0]);

    $.ajax({
        url: 'du.asp',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (returndata) {
            console.log(returndata);
        }
    });
Emre Erol
  • 470
  • 1
  • 7
  • 25
  • It been [detailed before](http://stackoverflow.com/q/12831680/692942) - glad you found the solution. – user692942 May 25 '16 at 22:20
  • Also [Sending multipart/formdata with jQuery.ajax](http://stackoverflow.com/a/5976031/692942) for another detailed breakdown. – user692942 May 25 '16 at 22:25