我看了這個問題的直接答案的高低。通過Ajax發送本地存儲的圖像到服務器
我在下面的目錄電話間隙應用本地加載圖像:
/image/test.png
我想這個圖像發送到服務器使用表單數據的多部分形式的一部分。
var formdata = new FormData();
我再追加我的JSON
formdata.append("json", JSON.stringify(request));
我則希望將圖像追加和我嘗試使用以下
var img = new Image();
img.src = './image/test.png';
formdata.append("file", img.src);
$.ajax(
{
url : "http://myserver:8080/myservlet",
type : "POST",
data : formdata,
processData : false,
contentType : false,
dataType : "json",
success : function(response)
{
console.log("success response:"+JSON.stringify(response));
},
error : function(jqXHR,textStatus,errorThrown)
{
var msg = "Error Sending File. Status: "+textStatus+" Error:"+errorThrown;
navigator.notification.alert(msg, null, "Error", "OK");
}
});
這如果課程不適合我的工作 - 我是什麼我做錯了。
注意,如果你從相機/ imageURI,使用上傳:'imageURI .substr(imageURI.lastIndexOf('/')+ 1)'獲取文件名。 – ocodo
感謝讓我檢查發送其他項目作爲參數,我假設這使用POST和表單數據類型在幕後傳輸。 – simapp
這是一篇文章,該文件將是一個Miltipart文件,但是你在服務器上實現 – DavidC