繁体   English   中英

使用 Dropbox JavaScript SDK 下载文件时出现问题

[英]Issues downloading files using Dropbox JavaScript SDK

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

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

我正在遵循以下文件中提供的文档:

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

我可以肯定地看到下载确实发生了,因为它显示在 Chrome 网络工具中,如下所示:

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

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

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

预先感谢您提供的任何帮助。

如果您希望在 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;

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

对于遇到此问题的任何其他人,您可以使用FileReader API。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM