在 JavaScript 中通過 XMLHttpRequest 從文字上載檔案
var path = "/test_javascript_upload.txt";
var content = "data to upload";
var accessToken = "<ACCESS_TOKEN>";
var uploadUrl = "https://content.dropboxapi.com/2/files/upload"
var result;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
result = xhr.responseText;
console.log(result);
}
};
xhr.open("POST", uploadUrl, true);
xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
xhr.setRequestHeader("Content-type", "application/octet-stream");
xhr.setRequestHeader("Dropbox-API-Arg", '{"path": "' + path + '"}');
xhr.send(content);
<ACCESS_TOKEN>
應替換為 OAuth 2 訪問令牌。