问题
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.
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 files included:
- jquery_ui_widget.js
- jquery_iframe_transport.js
- jquery_file_upload.js
- tmpl.js
The 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
- 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.
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.
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?
回答1:
From the documentation at https://github.com/blueimp/jQuery-File-Upload/wiki/Options.
Note: The maxNumberOfFiles option depends on the getNumberOfFiles option, which is defined by the UI and AngularJS implementations.
You will need to include one of those two for maxNumberOfFiles specifically. To get the rest working you will need fileupload-validate.js.
You might also need more than that. I recommend including all the optional js files, and then removing the unneeded ones.
回答2:
I think you will have to authenticate the user (Facebook, Google, Amazon), assign each user a sub folder in your bucket, and query the number of files the user already has in their folder before deciding whether to allow any more uploads.
来源:https://stackoverflow.com/questions/18551539/jquery-file-upload-how-to-limit-number-of-files-uploaded