簡體   English   中英

使用 Javascript Dropbox API 上傳帶有塊的大文件

[英]Uploading large files with chunks using Javascript Dropbox API

我正在使用此代碼通過 Javascript (vuejs) 將文件上傳到保管箱; 但是,我無法加載大於 350 MB 的文件。 我一直在嘗試使用塊來加載文件。 代碼沒有錯誤,但是當保管箱 api 返回結果時,我收到 400 錯誤:

Dropbox-sdk.min.js?0032:1 POSThttps://content.dropboxapi.com/2/files/upload_session/append_v2 400(錯誤請求)

我想知道代碼是否有問題,或者是否需要在 Dropbox 設置中更改某些內容? 我一直使用此代碼作為指南: https://github.com/dropbox/dropbox-sdk-js/blob/master/examples/javascript/upload/index.html#L2

       uploadToDropbox: function (path, file) {
            var dbx = this.dropbox()
            console.log("File upload .. " + path)
            console.log("File upload .. " + file)
            console.log(UPLOAD_FILE_SIZE_LIMIT)
            if (file.size < UPLOAD_FILE_SIZE_LIMIT) {
                this.dropbox().filesUpload({ path: path, contents: file })
                    //__PIPELINE\assets\test
                    .then(response => {
                        console.log(response)
                        //this.structure = response.result.entries;
                        console.log("This was successful")
                    })
                    .catch(error => {
                        console.log(error)
                        console.log("This is an error")
                    });
            }
            else {
                // File is bigger than 150 Mb - use filesUploadSession* API
                const maxBlob = 8 * 1000 * 1000; // 8Mb - Dropbox JavaScript API suggested max file / chunk size

                var workItems = [];

                var offset = 0;

                while (offset < file.size) {
                    var chunkSize = Math.min(maxBlob, file.size - offset);
                    workItems.push(file.slice(offset, offset + chunkSize));
                    offset += chunkSize;
                }
                console.log ("Work Items : ")
                console.log (workItems)
                const task = workItems.reduce((acc, blob, idx, items) => {
                    if (idx == 0) {
                        // Starting multipart upload of file
                        console.log("idx is 0")
                        return acc.then(function () {
                            return dbx.filesUploadSessionStart({ close: false, contents: blob })
                                .then(response => response.session_id)
                        });
                    } else if (idx < items.length - 1) {
                        console.log("idx is less than items.length")
                        // Append part to the upload session
                        return acc.then(function (sessionId) {
                           
                            var cursor = { session_id: sessionId, offset: idx * maxBlob };
                            return dbx.filesUploadSessionAppendV2({ cursor: cursor, close: false, contents: blob }).then(() => sessionId);
                        });
                    } else {
                        // Last chunk of data, close session
                        console.log("finishing session")
                        return acc.then(function (sessionId) {
                            var cursor = { session_id: sessionId, offset: file.size - blob.size };
                            var commit = { path: '/' + file.name, mode: 'add', autorename: true, mute: false };
                            return dbx.filesUploadSessionFinish({ cursor: cursor, commit: commit, contents: blob });
                        });
                    }
                }, Promise.resolve());

                task.then(function (result) {
                    console.log(result)
                    //var results = document.getElementById('results');
                    //results.appendChild(document.createTextNode('File uploaded!'));
                }).catch(function (error) {
                    console.error(error);
                });
            }
        },

代碼中缺少 session id。
響應 object 已在 sdk 的新版本中更新,示例代碼不再起作用:

https://github.com/dropbox/dropbox-sdk-js/blob/master/UPGRADING.md#4-updating-the-response-object

修復正在改變這一行:

.then(response => response.result.session_id)

以下是 github 上存在相同問題的線程的鏈接: https://github.com/dropbox/dropbox-sdk-js/issues/351

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM