使用Javascript Dropbox API上传带有区块的大文件



我使用这段代码用Javascript(vuejs(将文件上传到dropbox;然而,我一直无法加载大于350 MB的文件。我一直在尝试使用块来加载文件。代码没有错误,但当dropboxapi返回结果时,我得到了一个400错误:

Dropbox-sdk.min.js?0032:1员额https://content.dropboxapi.com/2/files/upload_session/append_v2400(错误请求(

我想知道代码是否有问题,或者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 })
//__PIPELINEassetstest
.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);
});
}
},

代码中缺少会话id
响应对象在新版本的sdk中更新,示例代码不再工作:

https://github.com/dropbox/dropbox-sdk-js/blob/master/UPGRADING.md#4-更新响应对象

修复程序正在更改这一行:

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

以下是github上一个有相同问题的线程的链接:https://github.com/dropbox/dropbox-sdk-js/issues/351

最新更新