简体   繁体   English

blob.slice()导致一个空的blob-javascript

[英]blob.slice() results in an empty blob - javascript

I am trying to send a large file to a server, so I am using the chunking technique in order to do it in a robust way. 我正在尝试将大文件发送到服务器,所以我正在使用分块技术以便以可靠的方式进行操作。

    private readonly sendChunk = (file: File, progressModel: ProgressResponseModel): void => {
        const offset = progressModel.Offset;
        if (offset > file.size)
            throw new Error("Offset cannot be greater than the file size");

        const expectedSize = progressModel.ExpectedChunkSize;

        const blobChunk = file.slice(offset, expectedSize);

        const xhr = new XMLHttpRequest();
        xhr.onload = (ev: Event): void => {
             if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
                    const progress = this.progressFromText(xhr.responseText);

                    if (progress.Offset >= 0) {
                        this.sendChunk(file, progress);
                    }

                    console.log(`${progress.Progress} %`);
                }
            }

        xhr.open("POST", this._uploadChunkUrl, true);
        xhr.send(blobChunk);
    }

The server sends back where to start the new chunk from and how big it should be. 服务器发回从何处开始新块,以及应从多大块开始。 The above function is executed in a recursive manner as you can see. 如您所见,以上功能以递归方式执行。

However, if the file requires more than 1 chunk to be sent, the second time I call const blobChunk = file.slice(offset, expectedSize); 但是,如果文件需要发送多个块,则第二次调用const blobChunk = file.slice(offset, expectedSize); I get an empty chunk (length 0). 我得到一个空块(长度为0)。

I can guarantee that the file arg is always valid (when console.log ed). 我可以保证file arg始终有效(当console.log ed时)。

I've seen this question , but I am sure my file is not removed or renamed. 我已经看到了这个问题 ,但是我确定我的文件没有被删除或重命名。 I've also seen this issue . 我也看到了这个问题 I get the same behavior for both Chrome and Firefox (latest versions), also Edge. 对于Chrome和Firefox(最新版本),以及Edge,我都得到相同的行为。

Thanks! 谢谢!

UPDATE Okay, so I did a dummy method in order to isolate this issue: 更新好的,所以我做了一个虚拟方法来隔离此问题:

    readonly chunkIt = (file: Blob): void => {
        var offset = 0;
        var length = 512 * 1024;

        while (offset >= 0) {
            const blobChunk = file.slice(offset, length);
            console.log(blobChunk);

            offset += length;
            if (offset > file.size) offset = -1;
        }
    }

And using it: 并使用它:

   $("input[name='fileUpload']").on("change", (e) => {
        const files = e.target.files;
        if (typeof files === "undefined" || files === null || files.length === 0)
            e.preventDefault();

        const file = files[0];

        this._client.chunkIt(file);
    });

Logs a correct Blob only the first time, the following are all empty. 仅第一次记录正确的Blob ,以下均为空。

SOLVED 解决了

From this issue - Splitting a File into Chunks with Javascript it turned out that I've forgot to offset my end index. 从这个问题开始- 使用Java脚本将文件拆分为块,结果是我忘了抵消end索引了。

You need to use splice() instead of slice() 您需要使用splice()而不是slice()

Replace 更换

const blobChunk = file.slice(offset, expectedSize);

with

const blobChunk = file.splice(offset, expectedSize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM