[英]Trigger a File Upload using AJAX without a form nor Jquery
這在以 JSON 格式獲取和發送文本時效果很好。 當使用表單、輸入和提交按鈕上傳圖像時,它也能很好地工作。
function ajaxPromise(url, send) {
return new Promise( function(resolve, reject) {
var XHR = new XMLHttpRequest();
XHR.open('POST', url);
XHR.onload = function() {
resolve(XHR.response); // Resolve promise with response
};
XHR.send(send);
});
}
需要幫助使其在不激活提交按鈕的情況下工作,例如,使用輸入的更改事件。
input.addEventListener('change', function(e) {
ajaxPromise('upload.php', file);
});
你能試試下面的代碼嗎
input.addEventListener('change', function(e) {
// this.files -- Array[Files] // when multiple files are selected.
// so Pick the first one
ajaxPromise('upload.php', this.files[0]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.