1

我正在尝试使用 Dropbox Javascript SDK 将文件下载到客户端的 Webapp 本身。

我想明确表示我只想将文件下载到网络应用程序的文件夹中;我了解,出于安全考虑,这实际上可能是不可能的。

我正在关注以下提供的文档:

http://dropbox.github.io/dropbox-sdk-js/index.html

http://dropbox.github.io/dropbox-sdk-js/Dropbox.html#filesDownload__anchor

这是我的控制器代码:

$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);
  });
};

我可以肯定地看到下载确实发生了,如下图所示:

(我没有足够的声誉来插入多个链接,所以请解释我生成的这个共享“链接”)

https://www.dropbox.com/s/s0gvpi4qq2nw23s/dbxFilesDownload.JPG?dl=0

我相信这要么是我缺乏处理文件下载的知识,要么是误用了 JavaScript。

提前感谢您提供的任何帮助。

4

2 回答 2

1

如果您希望在 Web 应用程序中下载和使用文件,那么最好设置一个后端服务器并使用它来临时存储内容,当然要获得用户的许可。

为此,请发出 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;
于 2017-02-07T11:16:26.553 回答
0

也有一种方法可以在客户端上执行此操作,而不必像公认的答案所建议的那样滚动您自己的服务器实现。

对于其他有此问题的人,您可以使用FileReaderAPI。

$scope.testDownload = function() {
  console.log('Testing Download');
  dbx.filesDownload( {path: '/Collorado Springs.jpg'} ) // Just a test file
    .then(function(response) {
      console.log(response);
      const reader = new FileReader();
      const fileContentAsText = reader.readAsText(response.result.fileBlob);
      reader.onload = (e) => {
        console.log({ file: reader.result }); // Logs file content as a string
      };
    })
    .catch(function(error) {
      console.log(err);
  });
};

于 2020-12-27T21:51:59.333 回答