Why can't I upload files asynchronously?

After a whole day of exploring && try, I finally refuse to upload files using pure AJAX (ps: this post How can I upload files asynchronously with JQuery? Buried my last hope)

My question may be a little pointless, but I still want to know why ajax (or XMLHttpRequest) can't handle this? why files cannot be transferred as real httprequest?

+5
source share
2 answers

Javascript cannot read local files for security reasons, so we cannot send data using AJAX.

HTML- POST iframe . iframe .

AJAX, , , , , , AJAX , HTTP, .

+6

, , - Googles , XMLHttpRequest 2 AJAX .

:

http://caniuse.com/xhr2

Javascript FormData.

FormData Ajax- jQuery?

processData contentType, .

var fd = new FormData();    
fd.append( 'file', input.files[0] );

$.ajax({
  url: 'http://example.com/script.php',
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }
});
+2

All Articles