如何上傳file對象

var formData = new FormData();
formData.append('file', file);
// 其餘些參數,例如用戶id
formData.append('userid', 1);
// ajax上傳
var xhr = null;

if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  xhr=new XMLHttpRequest();
}else{// code for IE6, IE5
  xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
// 上傳結束 
xhr.onload = function () { 
  var json = JSON.parse(xhr.responseText); // ... 這裏處理返回的json數據 
};
xhr.open('POST', './upload.php', true); 
xhr.send(formData);
相關文章
相關標籤/搜索