Javascript DataTransfer 项目不会通过异步调用持久化
Javascript DataTransfer items not persisting through async calls
我正在使用 Vuejs 和 DataTransfer 异步上传文件,我想允许多个文件被拖放一次上传。
我可以进行第一次上传,但是在上传完成时,Javascript 已经收集了垃圾或更改了 DataTransfer 项目对象。
我如何修改它(或克隆 event/DataTransfer 对象)以便在整个 ajax 调用期间我仍然可以使用数据?
我已经按照 MDN 文档了解如何使用 DataTransfer,但我很难将它应用到我的具体案例中。我也尝试过复制事件对象,正如您在我的代码中看到的那样,但它显然没有进行深度复制,只是传递引用,这没有帮助。
methods: {
dropHandler: function (event) {
if (event.dataTransfer.items) {
let i = 0;
let self = this;
let ev = event;
function uploadHandler() {
let items = ev.dataTransfer.items;
let len = items.length;
// len NOW EQUALS 4
console.log("LEN: ", len);
if (items[i].kind === 'file') {
var file = items[i].getAsFile();
$('#id_file_name').val(file.name);
var file_form = $('#fileform2').get(0);
var form_data = new FormData(file_form);
if (form_data) {
form_data.append('file', file);
form_data.append('type', self.type);
}
$('#file_progress_' + self.type).show();
var post_url = '/blah/blah/add/' + self.object_id + '/';
$.ajax({
url: post_url,
type: 'POST',
data: form_data,
contentType: false,
processData: false,
xhr: function () {
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function (event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
$('#file_progress_' + self.type).val(percent);
}
}, true);
}
return xhr;
}
}).done((response) => {
i++;
if (i < len) {
// BY NOW, LEN = 0. ????
uploadHandler();
} else {
self.populate_file_lists();
}
}
);
}
}
uploadHandler();
}
},
似乎 DataTransfer
的上下文随着时间的推移而丢失。我的解决方案是在丢失之前复制所需的数据,并在需要时重新使用它:
const files = [...e.dataTransfer.items].map(item => item.getAsFile());
使用我的解决方案修改了 jsfiddle of @Brad 中的代码:
const dropZone = document.querySelector(".dropZone");
const sendFile = file => {
const formData = new FormData();
for (const name in file) {
formData.append(name, file[name]);
}
/**
* https://docs.postman-echo.com/ - postman mock server
* https://cors-anywhere.herokuapp.com/ - CORS proxy server
**/
return fetch(
"https://cors-anywhere.herokuapp.com/https://postman-echo.com/post",
{
method: "POST",
body: formData
}
);
};
dropZone.addEventListener("dragover", e => {
e.preventDefault();
});
dropZone.addEventListener("drop", async e => {
e.preventDefault();
const files = [...e.dataTransfer.items].map(item => item.getAsFile());
const responses = [];
for (const file of files) {
const res = await sendFile(file);
responses.push(res);
}
console.log(responses);
});
body {
font-family: sans-serif;
}
.dropZone {
display: inline-flex;
background: #3498db;
color: #ecf0f1;
border: 0.3em dashed #ecf0f1;
border-radius: 0.3em;
padding: 5em;
font-size: 1.2em;
}
<div class="dropZone">
Drop Zone
</div>
调用 await
后,您将不再位于函数的原始调用堆栈中。这在事件监听器中尤为重要。
我们可以用setTimeout
重现同样的效果:
dropZone.addEventListener('drop', async (e) => {
e.preventDefault();
console.log(e.dataTransfer.items);
setTimeout(()=> {
console.log(e.dataTransfer.items);
})
});
例如拖拽四个文件会输出:
DataTransferItemList {0: DataTransferItem, 1: DataTransferItem, 2: DataTransferItem, 3: DataTransferItem, length: 4}
DataTransferItemList {length: 0}
事件发生后状态发生变化,物品丢失。
有两种方法可以解决这个问题:
- 复制项目并迭代它们
- 将异步作业(Promises)推送到数组中,稍后使用
Promise.all
处理它们
第二种解决方案比在循环中使用await
更直观。另外,请考虑 parallel connections are limited。使用数组,您可以创建块来限制同时上传。
function pointlessDelay() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
}
const dropZone = document.querySelector('.dropZone');
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
});
dropZone.addEventListener('drop', async (e) => {
e.preventDefault();
console.log(e.dataTransfer.items);
const queue = [];
for (const item of e.dataTransfer.items) {
console.log('next loop');
const entry = item.webkitGetAsEntry();
console.log({item, entry});
queue.push(pointlessDelay().then(x=> console.log(`${entry.name} uploaded`)));
}
await Promise.all(queue);
});
body {
font-family: sans-serif;
}
.dropZone {
display: inline-flex;
background: #3498db;
color: #ecf0f1;
border: 0.3em dashed #ecf0f1;
border-radius: 0.3em;
padding: 5em;
font-size: 1.2em;
}
<div class="dropZone">
Drop Zone
</div>
我 运行 遇到了这个问题,并且希望持久化整个 DataTransfer
对象,而不仅仅是 items
或 types
,因为我的异步代码 API 消耗 DataTransfer
类型本身。我最终做的是创建一个 new DataTransfer()
,并有效地复制原始属性(拖动图像除外)。
这是要点(在 TypeScript 中):https://gist.github.com/mitchellirvin/261d82bbf09d5fdee41715fa2622d4a6
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/kind
enum DataTransferItemKind {
FILE = "file",
STRING = "string",
}
/**
* Returns a properly deep-cloned object of type DataTransfer. This is necessary because dataTransfer items are lost
* in asynchronous calls. See
* for more details.
*
* @param original the DataTransfer to deep clone
*/
export function cloneDataTransfer(original: DataTransfer): DataTransfer {
const cloned = new DataTransfer();
cloned.dropEffect = original.dropEffect;
cloned.effectAllowed = original.effectAllowed;
const originalItems = original.items;
let i = 0;
let originalItem = originalItems[i];
while (originalItem != null) {
switch (originalItem.kind) {
case DataTransferItemKind.FILE:
const file = originalItem.getAsFile();
if (file != null) {
cloned.items.add(file);
}
break;
case DataTransferItemKind.STRING:
cloned.setData(originalItem.type, original.getData(originalItem.type));
break;
default:
console.error("Unrecognized DataTransferItem.kind: ", originalItem.kind);
break;
}
i++;
originalItem = originalItems[i];
}
return cloned;
}
您可以像这样使用它,然后按照您最初计划使用 evt.dataTransfer
:
的方式使用 clone
const clone = cloneDataTransfer(evt.dataTransfer);
我正在使用 Vuejs 和 DataTransfer 异步上传文件,我想允许多个文件被拖放一次上传。
我可以进行第一次上传,但是在上传完成时,Javascript 已经收集了垃圾或更改了 DataTransfer 项目对象。
我如何修改它(或克隆 event/DataTransfer 对象)以便在整个 ajax 调用期间我仍然可以使用数据?
我已经按照 MDN 文档了解如何使用 DataTransfer,但我很难将它应用到我的具体案例中。我也尝试过复制事件对象,正如您在我的代码中看到的那样,但它显然没有进行深度复制,只是传递引用,这没有帮助。
methods: {
dropHandler: function (event) {
if (event.dataTransfer.items) {
let i = 0;
let self = this;
let ev = event;
function uploadHandler() {
let items = ev.dataTransfer.items;
let len = items.length;
// len NOW EQUALS 4
console.log("LEN: ", len);
if (items[i].kind === 'file') {
var file = items[i].getAsFile();
$('#id_file_name').val(file.name);
var file_form = $('#fileform2').get(0);
var form_data = new FormData(file_form);
if (form_data) {
form_data.append('file', file);
form_data.append('type', self.type);
}
$('#file_progress_' + self.type).show();
var post_url = '/blah/blah/add/' + self.object_id + '/';
$.ajax({
url: post_url,
type: 'POST',
data: form_data,
contentType: false,
processData: false,
xhr: function () {
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function (event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
$('#file_progress_' + self.type).val(percent);
}
}, true);
}
return xhr;
}
}).done((response) => {
i++;
if (i < len) {
// BY NOW, LEN = 0. ????
uploadHandler();
} else {
self.populate_file_lists();
}
}
);
}
}
uploadHandler();
}
},
似乎 DataTransfer
的上下文随着时间的推移而丢失。我的解决方案是在丢失之前复制所需的数据,并在需要时重新使用它:
const files = [...e.dataTransfer.items].map(item => item.getAsFile());
使用我的解决方案修改了 jsfiddle of @Brad 中的代码:
const dropZone = document.querySelector(".dropZone");
const sendFile = file => {
const formData = new FormData();
for (const name in file) {
formData.append(name, file[name]);
}
/**
* https://docs.postman-echo.com/ - postman mock server
* https://cors-anywhere.herokuapp.com/ - CORS proxy server
**/
return fetch(
"https://cors-anywhere.herokuapp.com/https://postman-echo.com/post",
{
method: "POST",
body: formData
}
);
};
dropZone.addEventListener("dragover", e => {
e.preventDefault();
});
dropZone.addEventListener("drop", async e => {
e.preventDefault();
const files = [...e.dataTransfer.items].map(item => item.getAsFile());
const responses = [];
for (const file of files) {
const res = await sendFile(file);
responses.push(res);
}
console.log(responses);
});
body {
font-family: sans-serif;
}
.dropZone {
display: inline-flex;
background: #3498db;
color: #ecf0f1;
border: 0.3em dashed #ecf0f1;
border-radius: 0.3em;
padding: 5em;
font-size: 1.2em;
}
<div class="dropZone">
Drop Zone
</div>
调用 await
后,您将不再位于函数的原始调用堆栈中。这在事件监听器中尤为重要。
我们可以用setTimeout
重现同样的效果:
dropZone.addEventListener('drop', async (e) => {
e.preventDefault();
console.log(e.dataTransfer.items);
setTimeout(()=> {
console.log(e.dataTransfer.items);
})
});
例如拖拽四个文件会输出:
DataTransferItemList {0: DataTransferItem, 1: DataTransferItem, 2: DataTransferItem, 3: DataTransferItem, length: 4}
DataTransferItemList {length: 0}
事件发生后状态发生变化,物品丢失。
有两种方法可以解决这个问题:
- 复制项目并迭代它们
- 将异步作业(Promises)推送到数组中,稍后使用
Promise.all
处理它们
第二种解决方案比在循环中使用await
更直观。另外,请考虑 parallel connections are limited。使用数组,您可以创建块来限制同时上传。
function pointlessDelay() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
}
const dropZone = document.querySelector('.dropZone');
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
});
dropZone.addEventListener('drop', async (e) => {
e.preventDefault();
console.log(e.dataTransfer.items);
const queue = [];
for (const item of e.dataTransfer.items) {
console.log('next loop');
const entry = item.webkitGetAsEntry();
console.log({item, entry});
queue.push(pointlessDelay().then(x=> console.log(`${entry.name} uploaded`)));
}
await Promise.all(queue);
});
body {
font-family: sans-serif;
}
.dropZone {
display: inline-flex;
background: #3498db;
color: #ecf0f1;
border: 0.3em dashed #ecf0f1;
border-radius: 0.3em;
padding: 5em;
font-size: 1.2em;
}
<div class="dropZone">
Drop Zone
</div>
我 运行 遇到了这个问题,并且希望持久化整个 DataTransfer
对象,而不仅仅是 items
或 types
,因为我的异步代码 API 消耗 DataTransfer
类型本身。我最终做的是创建一个 new DataTransfer()
,并有效地复制原始属性(拖动图像除外)。
这是要点(在 TypeScript 中):https://gist.github.com/mitchellirvin/261d82bbf09d5fdee41715fa2622d4a6
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/kind
enum DataTransferItemKind {
FILE = "file",
STRING = "string",
}
/**
* Returns a properly deep-cloned object of type DataTransfer. This is necessary because dataTransfer items are lost
* in asynchronous calls. See
* for more details.
*
* @param original the DataTransfer to deep clone
*/
export function cloneDataTransfer(original: DataTransfer): DataTransfer {
const cloned = new DataTransfer();
cloned.dropEffect = original.dropEffect;
cloned.effectAllowed = original.effectAllowed;
const originalItems = original.items;
let i = 0;
let originalItem = originalItems[i];
while (originalItem != null) {
switch (originalItem.kind) {
case DataTransferItemKind.FILE:
const file = originalItem.getAsFile();
if (file != null) {
cloned.items.add(file);
}
break;
case DataTransferItemKind.STRING:
cloned.setData(originalItem.type, original.getData(originalItem.type));
break;
default:
console.error("Unrecognized DataTransferItem.kind: ", originalItem.kind);
break;
}
i++;
originalItem = originalItems[i];
}
return cloned;
}
您可以像这样使用它,然后按照您最初计划使用 evt.dataTransfer
:
clone
const clone = cloneDataTransfer(evt.dataTransfer);