我正在尝试使用 Dropbox Javascript SDK 将文件下载到客户端的 Web 应用程序本身。
我想明确表示,我只想将文件下载到网络应用程序中的文件夹中;我了解,出于安全考虑,这实际上可能是不可能的。
我正在遵循以下内容中提供的文档:
http://dropbox.github.io/dropbox-sdk-js/index.html
http://dropbox.github.io/dropbox-sdk-js/Dropbox.html#filesDownload__anchor
这是我的 Controller 代码:
$scope.testDownload = function() {
console.log('Testing Download');
dbx.filesDownload( {path: '/Collorado Springs.jpg'} ) // Just a test file
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(err);
});
};
我可以肯定地看到下载确实发生了,如 Chrome 网络工具中所示,如下所示:
(我没有足够的声誉来插入多个链接,因此请解释我生成的这个共享“链接”)
https://www.dropbox.com/s/s0gvpi4qq2nw23s/dbxFilesDownload.JPG?dl=0
我认为这要么是我缺乏文件下载方面的知识,要么是误用了 JavaScript。
预先感谢您提供的任何帮助。
最佳答案
如果您希望在网络应用程序中下载和使用文件,那么最好设置一个后端服务器并使用它来临时存储内容,当然需要用户的许可。
为此,请发出 HTTP 请求,然后使用 Express 通过调用 Dropbox 服务服务器端来处理该请求,然后使用如下所示的代码:
'use strict';
var Dropbox = require('dropbox');
var fs = require('fs');
var path = require('path');
exports.downloadFile = function(token, id, eventID, fileType, callback) {
var dbx = new Dropbox({ accessToken: token }); // creates post-auth dbx instance
dbx.filesDownload({ path: id })
.then(function(response) {
if(response.fileBinary !== undefined) {
var filepath = path.join(__dirname, '../../images/Events/' + eventID + '/' + fileType + '/Inactive/', response.name);
fs.writeFile(filepath, response.fileBinary, 'binary', function (err) {
if(err) { throw err; }
console.log("Dropbox File '" + response.name + "' saved");
callback('File successfully downloaded');
});
}
})
.catch(function(err) {
console.log(err);
callback('Error downloading file using the Dropbox API');
})
}
module.exports = exports;
关于javascript - 使用 Dropbox JavaScript SDK 下载文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39959444/