Upload File With Ajax XmlHttpRequest












51















Hi i am trying to send file with xmlhttprequest with this code.



<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>


but i got this error : the request was rejected because no multipart boundary was found
help me pls..










share|improve this question





























    51















    Hi i am trying to send file with xmlhttprequest with this code.



    <script>
    var url= "http://localhost:80/....";
    $(document).ready(function(){
    document.getElementById('upload').addEventListener('change', function(e) {
    var file = this.files[0];
    var xhr = new XMLHttpRequest();
    xhr.file = file; // not necessary if you create scopes like this
    xhr.addEventListener('progress', function(e) {
    var done = e.position || e.loaded, total = e.totalSize || e.total;
    console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
    }, false);
    if ( xhr.upload ) {
    xhr.upload.onprogress = function(e) {
    var done = e.position || e.loaded, total = e.totalSize || e.total;
    console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
    };
    }
    xhr.onreadystatechange = function(e) {
    if ( 4 == this.readyState ) {
    console.log(['xhr upload complete', e]);
    }
    };
    xhr.open('post', url, true);
    xhr.setRequestHeader("Content-Type","multipart/form-data");
    xhr.send(file);
    }, false);
    });
    </script>


    but i got this error : the request was rejected because no multipart boundary was found
    help me pls..










    share|improve this question



























      51












      51








      51


      18






      Hi i am trying to send file with xmlhttprequest with this code.



      <script>
      var url= "http://localhost:80/....";
      $(document).ready(function(){
      document.getElementById('upload').addEventListener('change', function(e) {
      var file = this.files[0];
      var xhr = new XMLHttpRequest();
      xhr.file = file; // not necessary if you create scopes like this
      xhr.addEventListener('progress', function(e) {
      var done = e.position || e.loaded, total = e.totalSize || e.total;
      console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
      }, false);
      if ( xhr.upload ) {
      xhr.upload.onprogress = function(e) {
      var done = e.position || e.loaded, total = e.totalSize || e.total;
      console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
      };
      }
      xhr.onreadystatechange = function(e) {
      if ( 4 == this.readyState ) {
      console.log(['xhr upload complete', e]);
      }
      };
      xhr.open('post', url, true);
      xhr.setRequestHeader("Content-Type","multipart/form-data");
      xhr.send(file);
      }, false);
      });
      </script>


      but i got this error : the request was rejected because no multipart boundary was found
      help me pls..










      share|improve this question
















      Hi i am trying to send file with xmlhttprequest with this code.



      <script>
      var url= "http://localhost:80/....";
      $(document).ready(function(){
      document.getElementById('upload').addEventListener('change', function(e) {
      var file = this.files[0];
      var xhr = new XMLHttpRequest();
      xhr.file = file; // not necessary if you create scopes like this
      xhr.addEventListener('progress', function(e) {
      var done = e.position || e.loaded, total = e.totalSize || e.total;
      console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
      }, false);
      if ( xhr.upload ) {
      xhr.upload.onprogress = function(e) {
      var done = e.position || e.loaded, total = e.totalSize || e.total;
      console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
      };
      }
      xhr.onreadystatechange = function(e) {
      if ( 4 == this.readyState ) {
      console.log(['xhr upload complete', e]);
      }
      };
      xhr.open('post', url, true);
      xhr.setRequestHeader("Content-Type","multipart/form-data");
      xhr.send(file);
      }, false);
      });
      </script>


      but i got this error : the request was rejected because no multipart boundary was found
      help me pls..







      javascript ajax jquery file-upload xmlhttprequest






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 20 '13 at 2:58







      user1823761

















      asked Jun 2 '11 at 6:19









      Sedat BaşarSedat Başar

      2,30121828




      2,30121828
























          1 Answer
          1






          active

          oldest

          votes


















          86















          1. There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way.


          2. xhr.send(file) doesn't send the file. You have to use the FormData object to wrap the file into a multipart/form-data post data object:



            var formData = new FormData();
            formData.append("thefile", file);
            xhr.send(formData);



          After that, the file can be access in $_FILES['thefile'] (if you are using PHP).



          Remember, MDC and Mozilla Hack demos are your best friends.



          EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.






          share|improve this answer





















          • 1





            ty so much that helped me..

            – Sedat Başar
            Jun 2 '11 at 11:48






          • 5





            xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

            – nkassis
            Apr 20 '12 at 19:19






          • 1





            @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

            – timdream
            Apr 22 '12 at 18:19











          • How come jquery can do it and pure javascript cannot?

            – John Smith
            Oct 18 '12 at 22:48






          • 2





            This method requires at least IE10 or Android 3.0.

            – andreszs
            Aug 5 '14 at 15:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6211145%2fupload-file-with-ajax-xmlhttprequest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          86















          1. There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way.


          2. xhr.send(file) doesn't send the file. You have to use the FormData object to wrap the file into a multipart/form-data post data object:



            var formData = new FormData();
            formData.append("thefile", file);
            xhr.send(formData);



          After that, the file can be access in $_FILES['thefile'] (if you are using PHP).



          Remember, MDC and Mozilla Hack demos are your best friends.



          EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.






          share|improve this answer





















          • 1





            ty so much that helped me..

            – Sedat Başar
            Jun 2 '11 at 11:48






          • 5





            xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

            – nkassis
            Apr 20 '12 at 19:19






          • 1





            @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

            – timdream
            Apr 22 '12 at 18:19











          • How come jquery can do it and pure javascript cannot?

            – John Smith
            Oct 18 '12 at 22:48






          • 2





            This method requires at least IE10 or Android 3.0.

            – andreszs
            Aug 5 '14 at 15:14
















          86















          1. There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way.


          2. xhr.send(file) doesn't send the file. You have to use the FormData object to wrap the file into a multipart/form-data post data object:



            var formData = new FormData();
            formData.append("thefile", file);
            xhr.send(formData);



          After that, the file can be access in $_FILES['thefile'] (if you are using PHP).



          Remember, MDC and Mozilla Hack demos are your best friends.



          EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.






          share|improve this answer





















          • 1





            ty so much that helped me..

            – Sedat Başar
            Jun 2 '11 at 11:48






          • 5





            xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

            – nkassis
            Apr 20 '12 at 19:19






          • 1





            @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

            – timdream
            Apr 22 '12 at 18:19











          • How come jquery can do it and pure javascript cannot?

            – John Smith
            Oct 18 '12 at 22:48






          • 2





            This method requires at least IE10 or Android 3.0.

            – andreszs
            Aug 5 '14 at 15:14














          86












          86








          86








          1. There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way.


          2. xhr.send(file) doesn't send the file. You have to use the FormData object to wrap the file into a multipart/form-data post data object:



            var formData = new FormData();
            formData.append("thefile", file);
            xhr.send(formData);



          After that, the file can be access in $_FILES['thefile'] (if you are using PHP).



          Remember, MDC and Mozilla Hack demos are your best friends.



          EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.






          share|improve this answer
















          1. There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way.


          2. xhr.send(file) doesn't send the file. You have to use the FormData object to wrap the file into a multipart/form-data post data object:



            var formData = new FormData();
            formData.append("thefile", file);
            xhr.send(formData);



          After that, the file can be access in $_FILES['thefile'] (if you are using PHP).



          Remember, MDC and Mozilla Hack demos are your best friends.



          EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 20 '13 at 2:43

























          answered Jun 2 '11 at 9:09









          timdreamtimdream

          4,30831722




          4,30831722








          • 1





            ty so much that helped me..

            – Sedat Başar
            Jun 2 '11 at 11:48






          • 5





            xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

            – nkassis
            Apr 20 '12 at 19:19






          • 1





            @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

            – timdream
            Apr 22 '12 at 18:19











          • How come jquery can do it and pure javascript cannot?

            – John Smith
            Oct 18 '12 at 22:48






          • 2





            This method requires at least IE10 or Android 3.0.

            – andreszs
            Aug 5 '14 at 15:14














          • 1





            ty so much that helped me..

            – Sedat Başar
            Jun 2 '11 at 11:48






          • 5





            xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

            – nkassis
            Apr 20 '12 at 19:19






          • 1





            @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

            – timdream
            Apr 22 '12 at 18:19











          • How come jquery can do it and pure javascript cannot?

            – John Smith
            Oct 18 '12 at 22:48






          • 2





            This method requires at least IE10 or Android 3.0.

            – andreszs
            Aug 5 '14 at 15:14








          1




          1





          ty so much that helped me..

          – Sedat Başar
          Jun 2 '11 at 11:48





          ty so much that helped me..

          – Sedat Başar
          Jun 2 '11 at 11:48




          5




          5





          xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

          – nkassis
          Apr 20 '12 at 19:19





          xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.

          – nkassis
          Apr 20 '12 at 19:19




          1




          1





          @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

          – timdream
          Apr 22 '12 at 18:19





          @nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.

          – timdream
          Apr 22 '12 at 18:19













          How come jquery can do it and pure javascript cannot?

          – John Smith
          Oct 18 '12 at 22:48





          How come jquery can do it and pure javascript cannot?

          – John Smith
          Oct 18 '12 at 22:48




          2




          2





          This method requires at least IE10 or Android 3.0.

          – andreszs
          Aug 5 '14 at 15:14





          This method requires at least IE10 or Android 3.0.

          – andreszs
          Aug 5 '14 at 15:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6211145%2fupload-file-with-ajax-xmlhttprequest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith