2

需要帮忙。

我正在使用 TUS 协议在 VIMEO 上上传视频。

我将视频文件分成块(大小:每个 300.000 字节)。

在第一个块之后,我看到他的大小是 300.000 字节;

...

var buf = new Int8Array(evt.target.result); var chunkSizeReal = buf.byteLength; // 300.000 字节
...

在第一次 PATCH 之后,我调用 HEAD 来检查 UploadOffset,我得到:

上传长度:68077489

上传偏移量:2609448

我不明白的是,如果块发送的大小为 300.000 字节,为什么偏移量是 2.609.448。

    function uploadingVideo(file, uploadLink){

                        //debugger;
                        $("#loader").show();
                        startTimer();

                        var fileSize   = (file.size - 1);
                        //var chunkSize  = 128000000; // 64 * 1024; // bytes
                        var chunkSize  = 300000; //30000000; //20 MB //2048 * 2048; //64 * 1024; // 64 * 1024; // bytes
                        var chunkOffset = 0;

                        var readEventHandler = function(evt) {

                            console.log("readEventHandler called");

                            if (evt.target.error === null) {
                                // offset += evt.target.result.length;
                                // console.log("Offset: " + offset);
                                // callback(evt.target.result); // callback for handling read chunk

                                console.log(evt.target);
                                //console.log(evt.target.result);

                                //var buf = new Uint32Array(evt.target.result);
                                var buf = new Int8Array(evt.target.result);

                                //chunkSize = parseInt(evt.target.result.length);
                                var chunkSizeReal = buf.byteLength;
                                console.log("ChunkSize: " + chunkSizeReal);

                                $.ajax({
                                    'url': uploadLink,
                                    'type': 'HEAD',
                                    'headers': {
                                        'Tus-Resumable': '1.0.0',
                                        'Accept': 'application/vnd.vimeo.*+json;version=3.4'
                                    },
                                    'success': function (data, text, jqXHR) {

                                        var uploadLength = jqXHR.getResponseHeader('Upload-Length');
                                        var uploadOffset = jqXHR.getResponseHeader('Upload-Offset');

                                        if(uploadLength === null && uploadOffset === null){
                                            uploadLength = 0;
                                            uploadOffset = 0;
                                        }

                                        console.log("UploadLength: " + uploadLength);
                                        console.log("UploadOffset: " + uploadOffset);

                                        var uplOffset = parseInt(uploadOffset);
                                        var uplLength = parseInt(uploadLength);

                                        if(uplOffset < uplLength) {

                                            if((uplOffset + chunkSizeReal) > uplLength){
                                                chunkSizeReal = uplLength - uplOffset;
                                                console.log("Last chunkSize: " + chunkSizeReal);
                                            }

                                            // console.log(evt.target.result);

                                            // console.log(typeof evt.target.result);
                                            // console.log(typeof file);

                                            console.log(evt.target.result);
                                            console.log(evt.target.result.length);

                                            $.ajax({
                                                'url': uploadLink,
                                                'type': 'PATCH',
                                                'headers': {
                                                    'Tus-Resumable': '1.0.0',
                                                    'Upload-Offset': uplOffset,
                                                    'Accept': 'application/vnd.vimeo.*+json;version=3.4',
                                                    'Content-Type': 'application/offset+octet-stream'
                                                },
                                                //'data': evt.target.result,
                                                'data': buf, //buf, // new Blob([buf])
                                                'success': function (result) {

                                                    console.log("Uploaded------------\n");
                                                    console.log("Offset + chunkSize: " + uplOffset + " - " + (uplOffset + chunkSizeReal));

                                                    // console.log(result);

                                                    //uplOffset += chunkSize;

                                                    //console.log("off + chunkS: " + uplOffset);

                                                    var uplOffsetX = uplOffset + chunkSizeReal;
                                                    var x = (uplOffsetX * 100 / fileSize).toFixed(0);

                                                    console.log(x + "%");
                                                    $("#percent").text(x + "%");

                                                    $("#UploadedSize").text(formatBytes(uplOffset + chunkSizeReal, 1)); 

                                                    $("#progress-bar").css("width", x + "%");
                                                    $("#progress-bar").data("aria-valuenow", x);


                                                    // Of to the next chunk
                                                    chunkReaderBlock(chunkOffset, chunkSize, file);

                                                },
                                                error: function (XMLHttpRequest, textStatus, errorThrown) {

                                                    if (textStatus == 'Unauthorized') {
                                                        //alert('custom message. Error: ' + errorThrown);
                                                        console.log('custom message. Error: ' + errorThrown);
                                                    } else {
                                                        //alert('custom message. Error: ' + errorThrown);
                                                        console.log('custom message. Error: ' + errorThrown);
                                                    }

                                                    clearInterval(timer);
                                                }
                                            });
                                        } else {
                                            console.log("Done reading file");
                                            return;
                                        }   

                                    }
                                });



                            } else {
                                console.log("Read error: " + evt.target.error);
                                return;
                            }
//                          if (offset >= fileSize) {
//                              console.log("Done reading file");
//                              return;
//                          }
                        }

                        chunkReaderBlock = function(_offset, chunkLength, _file) {
                            var r = new FileReader();

                            chunkOffset = _offset + chunkLength;

                            var blob = _file.slice(_offset, _offset + chunkLength);
                            r.onload = readEventHandler;
                            r.readAsArrayBuffer(blob);
                        }

                        // now let's start the read with the first block
                        chunkReaderBlock(chunkOffset, chunkSize, file);
4

1 回答 1

0

UploadOffset通信服务器已收到多少。设置块大小并不能保证每次迭代都“干净”地接收到每一口。

于 2020-02-21T21:48:55.873 回答