问题
I'm using plupload to upload images to Amazon S3. I have a method on the server which generates my signature and policy, which I call through Http GET.
This works just fine for uploading one filet at a time.
The problem is when I select multiple files to upload. I need to call my server method GetPolicy before each file upload, but the problem is that the upload sometime starts before I get the Response from GetPolicy. Here's a bit of code:
uploader.bind("FileFiltered", function (up, file) {
getAmazonUploadPolicty(function (response) {
uploader.settings.multipart_params.key = "test/" + response.FileId;
uploader.settings.multipart_params.policy = response.policy;
uploader.settings.multipart_params.signature = response.Signature;
uploader.settings.multipart_params.Filename = response.FileId;
});
});
uploader.bind("FilesAdded", function (up, files) {
setTimeout(function () {
uploader.start();
}, 200);
});
What I would need is to be able to trigger the upload programatically for a specific file in the call back of the getAmazonUploadPolicty method.
Any ideas? Thanks
回答1:
Found a solution.
I've created a method Get policies for several files - as many as the user has selected
getAmazonUploadPolicyItems(count, callback);
The server method returns a list of policies
And then I do this
var pocilites = { };
uploader.bind("BeforeUpload", function (up, file) {
// this fires before each file upload
var response = policies[file.id];
uploader.settings.multipart_params.key = response.key;
uploader.settings.multipart_params.policy = response.policy;
uploader.settings.multipart_params.signature = response.signature;
uploader.settings.multipart_params.Filename = response.filename;
});
uploader.bind("FilesAdded", function (up, files) {
getAmazonUploadPolicyItems(files.length, function (response) {
$.each(files, function(i, file) {
policies[file.id] = {
key: response[i].Key,
policy: response[i].Policy,
signature: response[i].Signature,
filename: response[i].Filename
};
});
uploader.start();
});
});
Hope this helps someone esle.
PS: For some unknown reason, sometimes when uploading several files I get "invalid signature" from Amazon, but on a second attempt it works fine. Any ideas?
回答2:
Sounds tricky.... I think you would be better off tossing the uploader.start into the FileFiltered command though in your amazon callback that way you're sure you get a response back and you can verify it's what you're after before you even try starting the upload.
uploader.bind("FileFiltered", function (up, file) {
getAmazonUploadPolicty(function (response) {
uploader.settings.multipart_params.key = "test/" + response.FileId;
uploader.settings.multipart_params.policy = response.policy;
uploader.settings.multipart_params.signature = response.Signature;
uploader.settings.multipart_params.Filename = response.FileId;
uploader.start();
});
});
This still doesn't solve the problem of your multi-file issue. I'm not aware of anything in plupload that allows you to just upload a specific file (though it sure would be nice) since the uploads are all managed as a queue when it comes time to upload. Theoretically maybe you could do something to create a new queue for each file? seems kinda odd there's nothing exposed to start an upload for a specific file
来源:https://stackoverflow.com/questions/22405440/upload-multiple-files-to-amazon-s3-using-plupload