[英]Send file by Ajax and FormData fail
我試圖在先前創建FormData的Jquery中使用Ajax發送文件。 這是我的代碼:
var inputFileImage = document.getElementById('uploadImage');
var file = inputFileImage.files[0];
var data = new FormData();
data.append('archivo',file);
jQuery.ajax({
url: '/services/rpc.php',
type: 'post',
data: {functionName: 'saveProfileImage', data : data},
contentType:false,
processData:false,
})
.done(function(response) {
alert(response);
}.bind(this))
.fail(function(response) {
alert('Sorry, there was an unexpected error, please try again later.\n\nInformation about the error:\n'+response);
});
此ajax始終會傳遞給fail
函數,如果我將processData
更改為true
則會返回另一個錯誤,提示錯誤:TypeError:'append'在未實現接口FormData的對象上調用。
謝謝你的幫助!
必須關閉processData才能發送二進制數據。 FormData元素完全用作二進制數據,而data:{}
需要處理。 在這種情況下,必須將附加參數附加到formData上!
var data = new FormData();
data.append('archivo',file);
data.append('functionName','saveProfileImage');
jQuery.ajax({
url: '/services/rpc.php',
type: 'post',
data: data,
contentType:false,
processData:false,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.