使用plupload上传多个文件到Amazon S3多个、上传、文件、plupload

2023-09-11 10:09:00 作者:Pride(傲骨)

我用plupload上传图片到Amazon S3。 我有我的生成签名和政策,这是我通过HTTP GET调用服务器上的方法。

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.

现在的问题是,当我选择要上传多个文件。 我需要每个文件上传前打电话给我服务器方法 GetPolicy ,但问题是,上传的某个时候开始之前,我从 GetPolicy 的响应。 这里有一点code:

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);
});

我需要的是能够以编程方式触发上传在回调的getAmazonUploadPolicty方法的具体文件。

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.

任何想法? 谢谢

推荐答案

找到了解决方案。

我创建了一个方法 获取政策的几个文件 - 尽可能多的用户选择了

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

然后,我这样做

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();
    });
});

希望这可以帮助别人esle。

Hope this helps someone esle.

PS:对于一些未知的原因,上传多个文件,有时当我从亚马逊获得无效签名,但在第二次尝试它工作正常。 任何想法?

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?

 
精彩推荐
图片推荐