使用 HTTP 从保管箱 API 读取下载的 blob
Read downloaded blob from dropbox API using HTTP
使用 Dropbox,您可以通过将 URL 拖放到 Dropbox 文件夹中来创建快捷方式。这将像这样保存:
使用来自 dropbox 的 /2/files/download HTTP API 将 return 一个看起来像这样的 XHR 响应:
如何解析此响应,以便您可以只抓取 URL 并使其成为可点击的 link?
这是 Angular 1 工厂需要的东西。要使用它,您只需从控制器调用 downloadFile 函数并提供文件在您的保管箱帐户中的路径。
function downloadFile(filePath) {
if (!filePath) {
console.error('Cannot download file because no file was specified.');
return;
}
return $q(function(fulfill, reject) {
$http({
url: 'https://content.dropboxapi.com/2/files/download',
method: 'POST',
headers: {
'Authorization': 'Bearer {{access-token-goes-here}}',
'Dropbox-API-Arg': `{"path": "${filePath}"}`
},
responseType: 'blob'
}).then(
results => {
// data received from dropbox is binary data saved as a blob
// The FileReader object lets web applications asynchronously read the contents of files
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader
var fileReader = new FileReader();
// function will run after successfully reading the file
fileReader.onload = function() {
var string = this.result; // store the file contents
string = encodeURI(string); // get rid of the paragraph return characters
var endPosition = string.indexOf('%0D%0A', 32); // find the end of the URL, startPosition is 32
var actualURL = string.substring(32, endPosition); // grab only the characters between start and end positions
fulfill(actualURL);
};
fileReader.readAsText(results.data);
},
error => reject(error));
});
}
使用 Dropbox,您可以通过将 URL 拖放到 Dropbox 文件夹中来创建快捷方式。这将像这样保存:
使用来自 dropbox 的 /2/files/download HTTP API 将 return 一个看起来像这样的 XHR 响应:
如何解析此响应,以便您可以只抓取 URL 并使其成为可点击的 link?
这是 Angular 1 工厂需要的东西。要使用它,您只需从控制器调用 downloadFile 函数并提供文件在您的保管箱帐户中的路径。
function downloadFile(filePath) {
if (!filePath) {
console.error('Cannot download file because no file was specified.');
return;
}
return $q(function(fulfill, reject) {
$http({
url: 'https://content.dropboxapi.com/2/files/download',
method: 'POST',
headers: {
'Authorization': 'Bearer {{access-token-goes-here}}',
'Dropbox-API-Arg': `{"path": "${filePath}"}`
},
responseType: 'blob'
}).then(
results => {
// data received from dropbox is binary data saved as a blob
// The FileReader object lets web applications asynchronously read the contents of files
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader
var fileReader = new FileReader();
// function will run after successfully reading the file
fileReader.onload = function() {
var string = this.result; // store the file contents
string = encodeURI(string); // get rid of the paragraph return characters
var endPosition = string.indexOf('%0D%0A', 32); // find the end of the URL, startPosition is 32
var actualURL = string.substring(32, endPosition); // grab only the characters between start and end positions
fulfill(actualURL);
};
fileReader.readAsText(results.data);
},
error => reject(error));
});
}