0

I have a form that can be submitted via form.submit() and the response is correct. Now I want to submit it using ajax, but I have a problem when submitting a file.

The form is pretty simple:

<form name="Upload" enctype="multipart/form-data" method="post" action="upload.asp"> <input type="file" name="file" id="fileinput"/> <input type="button" name="FileUpload" class="button" id="append_new" onclick="xmlhttpPost('upload.asp', document.getElementById('fileinput').files[0]);" value="submit file"/> </form> 

I got the ajax call as following:

function xmlhttpPost(strURL, form) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'multipart/form-data'); self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } self.xmlHttpReq.send('file=' + file); } function updatepage(str){ document.getElementById("fileitems").innerHTML = str; } 

The problem now is: the server gets the string [object file] rather than the actual file content. How can I make sure file data is submitted?

    2 Answers 2

    2

    You can use formData for that:

    function xmlhttpPost(strURL, form) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'multipart/form-data'); self.xmlHttpReq.onreadystatechange = function () { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } if ( !! window.FormData) { var formData = new FormData(); formData.append('file', form); self.xmlHttpReq.send(formData); } } function updatepage(str) { document.getElementById("fileitems").innerHTML = str; } 

    Here's a decent example of a full ajax file uploader with progress.

      0

      You are missing this:

      self.xmlHttpReq.setRequestHeader("X_FILENAME", file.name); 

      That will differentiate between a normal POST and a file upload for the app tier.

      If you are using PHP, it would look something like:

      $file = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : ''); 
      1
      • there is no need to set a filename in the header. you may note i changed the content-type to "multipart/form-data". FYI i handle this with asp classic (hating it myself), and thus do not need further parameters.
        – Vogel612
        CommentedMar 27, 2013 at 7:19

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.