jQuery的文件上传 - 如何限制文件的数量上传文件上传、数量、上传、文件

2023-09-11 23:48:40 作者:浅笑梨窝少年

我想从blueimp jQuery的文件上传。该文件说,我应该能够限制与maxNumberOfFiles设置上传的文件数量。

I am trying out Jquery File Upload from blueimp. The documentation says I should be able to limit the number of files to be uploaded with the maxNumberOfFiles setting.

不过,这似乎并不在我的情况是工作。概无其他如acceptFileTypes,loadImageMaxFileSize等似乎工作是(可能是我必须包括其他的js文件 - ?真的不知道哪些或如何挂钩时间了,尽管研究了很多)。我现在用的是插件的基本版本。下面是我的code相:

However, that doesn't seem to be working in my case. None of the others such as acceptFileTypes, loadImageMaxFileSize etc seem to be working either (probably I have to include other js files? - don't know really which ones or how to hook em up despite researching quite a lot) . I am using the basic version of the plugin. Here is how my code looks:

JS文件包括:

jquery_ui_widget.js jquery_iframe_transport.js jquery_file_upload.js tmpl.js

在JS code:

$('#fileupload').fileupload
  maxFileSize: 3 * 1024
  minFileSize: 1
  maxNumberOfFiles: maxFilesAllowed
  autoUpload: false
  loadImageMaxFileSize: 3 * 1024
  acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
  add: (e, data) =>
    console.log 'add callback triggered'

该插件回调触发每个文件。即使我尝试实现自己的解决方案,以限制上传文件数,我希望能够知道当所有的调用增加是否齐全。换句话说,如果我要实现的解决方案,它看起来像

The add callback is triggered for EVERY file. Even if I try to implement my own solution to limit the number of uploaded files, I would like to be able to know when all the calls to "add" are complete. In other words, if I have to implement the solution, it would look like

在等待add事件为每个文件。维护一个计数器来计算对添加回调每个文件 知道什么时候都添加事件的触发是完整的所有文件 - 如何??? 在看看有多少文件被上传和prevent上传所有文件,如果他们超过允许的最大数量,并提示从最初选择上传选择的文件的子集。 Wait for add event on for each file. Maintain a counter to count each file on the add callback Know when all add event triggering is complete for ALL files -- HOW??? See how many files were uploaded and prevent upload of all the files if they exceed the max number allowed and prompt for selecting a subset of files from initial selection to upload.

解决方案是纯粹的客户端的图像和照片直接上传到S3,但我需要prevent超过3上传的每一笔交易。与当前执行我,没有什么是$ P $从具有每吨图像中选择他/她的整个图像文件夹,并陷入困境的S3存储pvents用户。因为在当时的政策允许范围内上传亚马逊的部分将采取尽可能多的文件。

The solution is purely client side for the images and the images directly upload to S3 but I need to prevent more than 3 uploads for each transaction. With the currently implementation I have, there is nothing that prevents the user from selecting his/her whole image folder and mess up the S3 bucket with ton of images. Amazon for its part will take as many files as are uploaded within the time the policy allows.

唯一的出路这种纠葛似乎是路由通过我们的服务器,而我试图拼命避免由于可伸缩性/性能问题都上传。我们是一个非常小的启动用非常小的缩放能力在这个时候。其他的想法我是要求服务器为每一个文件的上传,这似乎相当麻烦的30秒启动的政策。

The only way out of this imbroglio seems to be to route all uploads through the our servers which I am trying to desperately avoid due to scalability/performance issues. We are a very tiny startup with very minimal scaling capacity at this time. The other idea I have is to ask the server for a 30 second enabled policy for every file upload which seems rather cumbersome.

任何其他的想法/方案/建议/人试图/使用的工作建议?

Any other ideas/alternatives/suggestions/recommendations that people have tried/used that worked?

推荐答案

从文档的的 https://github.com/blueimp/jQuery-File-Upload/wiki/Options 。

注:maxNumberOfFiles选项依赖于getNumberOfFiles   选项​​,这是由用户界面和AngularJS实现定义

Note: The maxNumberOfFiles option depends on the getNumberOfFiles option, which is defined by the UI and AngularJS implementations.

您将需要包括两个用于maxNumberOfFiles专门之一。 要获得剩下的工作,你需要文件上传,validate.js。

You will need to include one of those two for maxNumberOfFiles specifically. To get the rest working you will need fileupload-validate.js.

您可能还需要不止于此。我建议,包括所有可选的js文件,然后删除不需要的那些。

You might also need more than that. I recommend including all the optional js files, and then removing the unneeded ones.