【问题标题】:Dropzone chunk uploading to Azure StorageDropzone 块上传到 Azure 存储
【发布时间】:2019-04-13 05:50:59
【问题描述】:

我正在尝试使用 dropzone 使用 SAS(共享访问签名)将大文件直接上传到 Azure 存储。这记录在 Azure 方面:https://docs.microsoft.com/en-us/rest/api/storageservices/Put-Block

所以我必须获取blockId(一个用于标识我正在发送的块的Base64字符串)并将其放入发送该块的请求的url中。

Dropzone 现在支持分块,所以我决定使用它。不幸的是,很难找到它的实现。

我可以在处理事件中更改 url,但这是针对每个文件的,我无法从中获取块数据。

我可以使用 params 在表单数据中发送 blockId,但似乎无法从中更改 url。

是否可以将 blockId 添加到我的网址中?还是我必须先将其发送到我的服务器并从那里上传?谢谢。

$("#videoSection").dropzone({
            params: function (files, xhr, chunk) {
                console.log(chunk);

                xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');

                //I can get the blockId from the chunk here

                //this.options.url.replace("test") //doesn't work
                //xhr.open(this.options.method, this.options.url.replace("test"), true); //doesn't work

                return {"url": "test"}; //this returns form-data
            },
            url: "Create",
            method: "PUT",
            //headers: { "x-ms-blob-type": "BlockBlob" },
            chunking: true,
            chunkSize: 4000000,
            forceChunking: true,
            retryChunks: true,
            retryChunksLimit: 3,
            autoProcessQueue: false,
            acceptedFiles: "video/*",
            maxFiles: 1,
            maxFilesize: 3000,
            previewTemplate: $("#videoTemplate").html(),
            dictDefaultMessage: "Drop Video Here",
            init: function () {
                this.on("processing", function (file) {
                    var blockId = 1;

                    @*this.options.url = "@(Config.Value.StoragePrefix)/@(user.Company.StorageName)/inspections/@Model.InspectionData.Inspection.Id/videos/"
                        + file.name + "@Html.Raw(Model.SharedAccessSignature + "&comp=block&blockid=")" + blockId;*@


                    var progressBar = $(file.previewElement).find(".dropzoneProgressBar");
                    progressBar.show();
                });
                this.on("chunksUploaded", function (file, done) {
                    $.ajax({
                        type: "PUT",
                        url: "@(Config.Value.StoragePrefix)/@(user.Company.StorageName)/inspections/@Model.InspectionData.Inspection.Id/videos/"
                            + file.name + "@Html.Raw(Model.SharedAccessSignature + "&comp=blocklist")",
                        success: function (data) {
                            done();
                        },
                        error: function (e) {
                            toastr.error(e);
                            console.log(e);
                        }
                    });
                });
                this.on("uploadprogress", function (file, progress, bytesSent) {
                    var progressBar = $(file.previewElement).find(".dropzoneProgressBar");
                    progress = bytesSent / file.size * 100;
                    progressBar.width(progress + "%");
                });
                this.on("success", function (file, response) {
                    var successCheckmark = $(file.previewElement).find(".dropSuccess");
                    successCheckmark.toggle();
                    var progressBar = $(file.previewElement).find(".dropzoneProgressBar");
                    progressBar.css("background-color", "green");
                });
            }
        });

【问题讨论】:

    标签: javascript xmlhttprequest dropzone.js


    【解决方案1】:

    this.options.url.replace("test") //doesn't work 这一行有问题。应该是this.options.url = this.options.url.replace("test", "newValue") //this should work。我正在使用类似的方法。问题是一旦打开XmlHttpRequest 就会调用params 回调,因此您需要在开始处理文件之前设置第一个块的url,并且在params 中您需要设置下一个块的url要发送的块(使用chunk.index + 1 进行基于零的索引)。如果您启用了parallelChunkUploads,我不确定这是否可行 - 我尚未测试要处理的第一个块是否是第一个块。如果您需要更多信息,请告诉我。

    【讨论】:

    • 我实际上已经切换到 FineUploader,它内置了 azure 支持和一些其他漂亮的功能,如初始文件列表。当然,在我实现它 18 小时后,作者停止维护它,但它适用于我目前的目的。不过,感谢您对 dropzone 的提醒。我会将此标记为答案。
    猜你喜欢
    • 2014-08-23
    • 2020-08-12
    • 2021-08-02
    • 1970-01-01
    • 2014-08-16
    • 2023-03-28
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多