目前,Dropbox API允许浏览器通过脚本访问(读/写)本地驱动器上的Dropbox文件夹中的所有类型的文件。可以在脚本中指定文件路径和名称。该脚本适用于存储在本地驱动器上的网页,但也适用于使用Chrome时存储在远程网站上的网页。
请使用原始实现来查看这些jsfiddles,以测试在本地dropbox文件夹中写入和读取文本文件。这些脚本是用纯粹的vanilla javascript代码编写的,不需要库。 只需要一次性访问令牌,无需进一步的身份验证过程。
这个jsfiddle(点击)演示了:Write text file to local dropbox folder
function WriteFile() {
var H = new Array(6);
H[0] = "Authorization";
H[1] = document.getElementById("bear").value;
H[2] = "Content-type";
H[3] = "application/octet-stream";
H[4] = "Dropbox-API-Arg";
H[5] = '{"path": "/' + document.getElementById("pana").value;
H[5] = H[5] + '","mode":{".tag":"overwrite"}}';
var X = new XMLHttpRequest();
X.onreadystatechange = acb;
X.open("POST", "https://content.dropboxapi.com/2/files/upload", true);
X.setRequestHeader(H[0], H[1]);
X.setRequestHeader(H[2], H[3]);
X.setRequestHeader(H[4], H[5]);
X.send(document.getElementById("myText").value);
document.getElementById("demo").innerHTML = "writing . . .";
}
function acb(A) {
var X = A.target;
var G = X.readyState;
if (G != 4) {
return 0;
}
document.getElementById("demo").innerHTML = "status: " + X.status;
}
这个jsfiddle(点击)演示了:Read text file from local dropbox folder
function ReadFile() {
document.getElementById("text").value = "";
var H = new Array(6);
H[0] = "User-Agent";
H[1] = "api-explorer-client";
H[2] = "Authorization";
H[3] = document.getElementById("bear").value;
H[4] = "Content-type";
H[5] = "application/json";
var X = new XMLHttpRequest();
X.onreadystatechange = acb;
X.open("POST", "https://api.dropboxapi.com/2/sharing/create_shared_link", true);
X.setRequestHeader(H[0], H[1]);
X.setRequestHeader(H[2], H[3]);
X.setRequestHeader(H[4], H[5]);
X.send('{"path": "/' + document.getElementById("pana").value + '"}');
document.getElementById("stat").innerHTML = "Reading . . .";
}
// async call back 1
function acb(A) {
var X = A.target;
if (X.readyState != 4) {
return 0; // when ready
}
var stat = X.status;
document.getElementById("stat").innerHTML = "status: " + stat;
if (stat != 200) {
document.getElementById("text").value = "NOT FOUND";
return 0; // error
}
var B = X.responseText;
var C = JSON.parse(B);
var D = C.url;
var E = D.split(".com");
var F = E[1];
var G = "https://dl.dropboxusercontent.com" + F;
document.getElementById("text").value = G;
X = new XMLHttpRequest();
X.onreadystatechange = acb2;
X.open("GET", G, true);
X.send(null);
}
// async callback 2
function acb2(A) {
var X = A.target;
if (X.readyState != 4) {
return 0; // when ready
}
var stat = X.status;
if (stat != 200) {
document.getElementById("text").value = "Can't Read the file";
return 0; // error
}
var B = X.responseText;
document.getElementById("text").value = B;
}
我尝试使用“google drive API”为本地驱动器上的“google drive”中的文件获取相同的功能,但没有成功。有人可以实现这一点,最终使用其他云存储系统。