0

目前,保管箱 API 允许浏览器通过脚本访问(读/写)本地驱动器上保管箱文件夹中的所有类型的文件。文件路径和名称可以在脚本中指定。该脚本适用于存储在本地驱动器上的网页,但也适用于使用 Chrome 时存储在远程站点上的网页。

请查看这些带有原始实现的 jsfiddles,以测试在本地保管箱文件夹中写入和读取文本文件。这些脚本是用纯 JavaScript 代码编写的,不需要库。只需要一次性访问令牌,无需进一步的身份验证程序。

此 jsfiddle(单击)演示:将文本文件写入本地保管箱文件夹

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(单击)演示:从本地保管箱文件夹中读取文本文件

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”中的文件获取相同的功能,但没有成功。有人可以做到这一点,最终使用其他云存储系统。

4

0 回答 0