希望這可以幫助未來的人。
你需要像這樣
var canvas = document.getElementById("paint");
然後
function uploadFile() {
var ACCESS_TOKEN = "xxx";
var dbx = new Dropbox({
accessToken: ACCESS_TOKEN
});
var fileInput = document.getElementById('file-upload');
var file = fileInput.files[0];
//Get data from canvas
var imageSringData = canvas.toDataURL('image/png');
//Convert it to an arraybuffer
var imageData = _base64ToArrayBuffer(imageSringData);
dbx.filesUpload({
path: '/' + "test.png",
contents: imageData
})
.then(function(response) {
var results = document.getElementById('results');
results.appendChild(document.createTextNode('File uploaded!'));
console.log(response);
})
.catch(function(error) {
console.error(error);
});
return false;
}
function _base64ToArrayBuffer(base64) {
base64 = base64.split('data:image/png;base64,').join('');
var binary_string = window.atob(base64),
len = binary_string.length,
bytes = new Uint8Array(len),
i;
for (i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
你有任何代碼,你沒試過嗎?你真的必須將它編碼爲base64編碼的字符串嗎?您應該可以將其作爲常規Blob文件上載。 –
不,我不必將它編碼爲base64字符串。 是的,我可以提供一些代碼,只是我仍然在POC階段,我正在探索不同的方法。 – SunnyHoHoHo
[官方Dropbox API v2 JavaScript SDK](https://github.com/dropbox/dropbox-sdk-js)和[本示例](https://github.com/dropbox/dropbox-sdk-js/blob /master/examples/upload/index.html#L49)可能是一個很好的起點。 – Greg