1 回答
TA贡献1995条经验 获得超2个赞
我能够自己解决这个问题。以下是方法(请注意,这需要一些修改才能适用于您自己的代码,我不是纯粹的复制和粘贴答案):
创建一个数组来保存一些数据:
/** Files currently uploading */
const uploadQueue: any[] = [];
捕获FileSuccess事件并添加它
// Check the size of the queue
if (uploadQueue.length < 20) {
const that = (uploadSuccessEvent.sender as any);
const module = that._module;
const upload = module.upload;
$(".k-file").each((i, x) => {
//console.log(i, x);
if (uploadQueue.length < 20) {
const fileEntry = $(x);
const started = fileEntry.is(".k-file-progress, .k-file-success, .k-file-error");
const hasValidationErrors = upload._filesContainValidationErrors(fileEntry.data("fileNames"));
if (!started && !hasValidationErrors) {
uploadQueue.push(fileEntry.data("fileNames")[0].uid);
//console.log("fileEntry", fileEntry.data("fileNames")[0].uid);
// Start the upload process
module.performUpload(fileEntry);
}
}
else { return; }
});
}
创建一个新函数来处理上传队列:
/**
* Adds the file to the upload queue and starts the upload.
* Other files will be loaded via the on success event.
* @param uploadSelectEvent Select event object.
*/
function queueUpload(uploadSelectEvent: kendo.ui.UploadSelectEvent) {
//console.log("uploadSelectEvent", uploadSelectEvent);
// Check the size of the queue
if (uploadQueue.length < 20) {
// Start the upload process
const that = (uploadSelectEvent.sender as any);
const module = that._module;
const upload = module.upload;
//uploadSelectEvent.files.forEach((file, i) => { console.log(i, file); if (uploadQueue.length < 20) { uploadQueue.push(file.uid); } });
$(".k-file").each((i, x) => {
//console.log(i, x);
if (uploadQueue.length < 20) {
const fileEntry = $(x);
const started = fileEntry.is(".k-file-progress, .k-file-success, .k-file-error");
const hasValidationErrors = upload._filesContainValidationErrors(fileEntry.data("fileNames"));
if (!started && !hasValidationErrors) {
uploadQueue.push(fileEntry.data("fileNames")[0].uid);
module.performUpload(fileEntry);
}
}
else { return; }
});
}
}
捕获FileSelect事件并将事件传递给queueUpload.
最后,您应该一次将并发上传限制为 20 个。它仍然允许将 100 个或 1000 个文件拖入浏览器(它可能仍会锁定一秒钟),但一次最多只能创建 20 个连接。这可能不是最理想的代码,但它适用于我的情况。
添加回答
举报