[英]Trigger a File Upload using AJAX without a form nor Jquery
this works well when Getting and Sending text in JSON format.这在以 JSON 格式获取和发送文本时效果很好。 It also works well when uploading images using a form an input and a submit button.
当使用表单、输入和提交按钮上传图像时,它也能很好地工作。
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);
});
}
Need help in making it work without activating the submit button, for example, with the change event of the input.需要帮助使其在不激活提交按钮的情况下工作,例如,使用输入的更改事件。
input.addEventListener('change', function(e) {
ajaxPromise('upload.php', file);
});
Can you try the below code你能试试下面的代码吗
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.