我有一个表单生成器,我想添加拖放文件功能。
我遇到的问题是我遇到的所有资源都是通过 xhr
上传图像的下降时。我希望图像一直保留到提交表单为止。
理想情况下 event.dataTransfer.files[0]
对象将被转移到 <input type="file" ...value="[dropped-file]">
元素。
目前我无法做到这一点。他们使用兼容的数据类型吗?
最佳答案
您可以在放置事件发生后创建并发送一个表单,然后用户单击一个 conf 按钮。这是它的要点:(未测试)。
function uploadFile(file) {
var form = new FormData(),
xhr = new XMLHttpRequest();
form.append('media', file);
xhr.open('POST', '/myurl/');
xhr.onprogress = function(e) {
showProgress();
}
xhr.onload = function(e) {
showSuccessConf();
}
xhr.send(form);
}
uploadFile(event.dataTransfer.files[0]);
关于javascript - 将拖放文件发送到输入类型文件元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17866254/