简体   繁体   English

在 FineUploader 中预处理文件期间如何显示进度?

[英]How to show progress during preprocessing of files in FineUploader?

I apply some preprocessing to files when uploading them with FineUploader (to Azure).在使用 FineUploader(到 Azure)上传文件时,我对文件进行了一些预处理。 Specifically, this means calculating the file's MD5 hash and possibly editing some specific elements of the bytes.具体来说,这意味着计算文件的 MD5 哈希值并可能编辑字节的某些特定元素。 I've implemented this and attached my code to the onSubmit event .我已经实现了这个并将我的代码附加到onSubmit 事件

Unfortunately, this process can take quite a lot of time, and users typically submit anywhere between 18 to 2000 files simultaneously.不幸的是,这个过程可能需要相当长的时间,而且用户通常同时提交 18 到 2000 个文件。 FineUploader doesn't add them to the UI nor does it show progress at this stage, leaving my users in the dark. FineUploader 不会将它们添加到 UI 中,也不会在此阶段显示进度,让我的用户一无所知。 They will add more files, thinking that they did something wrong, and make other incorrect decisions.他们会添加更多文件,认为他们做错了什么,并做出其他错误的决定。

I already intend to do the processing with a queue of web workers, but this will only makes the UI more responsive, it won't help showing some feedback to my users.我已经打算使用 Web 工作者队列进行处理,但这只会使 UI 更具响应性,它无助于向我的用户显示一些反馈。 I'm wondering if I should switch to a different event such as onValidate , or if there is some other approach I could consider.我想知道是否应该切换到不同的事件,例如onValidate ,或者是否还有其他一些我可以考虑的方法。 What do you think?你怎么认为?

Showing progress was easy.显示进展很容易。 I added the following HTML to the template:我在模板中添加了以下 HTML:

<span class="preprocessing-selector qq-drop-processing qq-hide">
    <span>Pre-processing files...</span>
    <span class="preprocessing-spinner-selector qq-drop-processing-spinner"></span>
</span>

In the onFinish handler of my custom processing I added $(".preprocessing-selector").addClass("qq-hide");在我的自定义处理的onFinish处理程序中,我添加了$(".preprocessing-selector").addClass("qq-hide"); and I added $(".preprocessing-selector").removeClass("qq-hide");我添加了$(".preprocessing-selector").removeClass("qq-hide"); to the onSubmit handler of FineUploader.到 FineUploader 的onSubmit处理程序。

That was sufficient to indicate progress.这足以表明取得了进展。


For those interested in knowing how I added preprocessing into the pipeline, here's the grand overview and explanation.对于那些有兴趣了解我如何将预处理添加到管道中的人,这里是总体概述和解释。 As far as I can tell from our preliminary test results there are no performance issues.据我所知,从我们的初步测试结果来看,没有性能问题。

总览

I used a gist called workcrew.js and applied some changes to use it for my purposes.我使用了一个名为workcrew.js的要点并应用了一些更改以将其用于我的目的。 It manages a pool of Web Workers and provides a simple API.它管理一个 Web Workers 池并提供一个简单的 API。 I used the onFinish event to hide the progress indicator.我使用onFinish事件来隐藏进度指示器。

Whenever a file is submitted, I pass it to workcrew.js for preprocessing and return a promise to FineUploader.每当提交文件时,我都会将其传递给 workcrew.js 进行预处理,并向 FineUploader 返回一个 promise。 The workers return the MD5 hash, and, if preprocessing was necessary, the modified File object and its new MD5 hash.工作人员返回 MD5 哈希值,如果需要预处理,则返回修改后的File对象及其新的 MD5 哈希值。

  • If we've seen this MD5 hash before (duplicate) we reject the promise.如果我们之前(重复)见过这个 MD5 哈希,我们拒绝承诺。
  • If we got a modified File object, we reject the promise and submit the new file using addUploads .如果我们得到一个修改过的File对象,我们拒绝承诺并使用addUploads提交新文件。
  • Otherwise we set the MD5 hash to benefit from Azure's MD5 hash verification mechanism and resolve the promise.否则,我们将 MD5 哈希设置为受益于 Azure 的 MD5 哈希验证机制并解决承诺。

One catch: you have to make sure to use unshiftWork instead of pushWork when sending a file to workcrew.js, so that processed files that have been resubmitted are handled first.一个陷阱:你必须确保使用unshiftWork代替pushWork发送文件到workcrew.js时,使已重新提交处理的文件被优先处理。 That way processing and uploading happens concurrently.这样处理和上传是同时发生的。 Otherwise you'll be processing all submitted files first, and then uploading, which is way less efficient.否则,您将首先处理所有提交的文件,然后再上传,这会降低效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM