我如何通过javaScript从dropbox下载图像?



我试过这种方式,但不适合我。实际上,我想要的是从dropbox下载一张图片并保存在文件夹中,然后显示在我的UI中。但我不能弄清楚为什么这个方法不工作,或者这是从使用javascript下载图像dropbox的正确方法?

// <--------------dropbox code start here------------->
let options = {
success: function(files) {
console.log("files", files);
let imageLink = files[0].link;
var array = imageLink.split("dl=0"); 
var splitLink=array[0];
let finalLink=splitLink+"raw=1";
console.log("Final Link",finalLink);
document.getElementById("step-two-rtsp-video").style.display = "none";
document.getElementById('output').style.display = "none";
document.getElementById("take-picture-btn").style.display = "none";
const newImage = document.createElement("img");
newImage.src = finalLink;
document
.getElementById("uploaded-img")
.appendChild(newImage);

var path = files[0].name;
var token = "token";
var url = "https://api-content.dropbox.com/1/files/auto/" + path;
var result;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
result = xhr.responseText;
console.log(result);
}
}
xhr.open("GET", url, true);
xhr.setRequestHeader("access_token", token);
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.send();


},
cancel: function() {
//optional
},
linkType: "preview", // "preview" or "direct"
multiselect: true, // true or false
extensions: ['.png', '.jpg'],
};
var button = Dropbox.createChooseButton(options);
document.getElementById("selectingFileType").appendChild(button);

// <--------------dropbox code end here-------------> 

基于"/1/"的Dropbox API URL,我看到你正在尝试调用Dropbox API v1,它已经退役。你应该使用Dropbox API v2。

对于使用Dropbox API v2与JavaScript,我建议使用官方Dropbox API v2 JavaScript SDK。README中有安装和使用它的信息和示例。

使用filesDownload方法下载文件。

最新更新