首页 > 解决方案 > 从 Dropbox 下载的空白图像和 pdf

问题描述

从我的 Dropbox 下载文件时出现问题。
使用“文本/csv”类型时,我可以下载和查看 txt 文件。
将类型更改为“image/jpeg”或“application/pdf”并下载文件类型会给我空白文件。

我是在正确的轨道上还是应该这样做?

主服务.ts

downloadFile(id) {
  const headers = new Headers();
  headers.append('Authorization', 'Bearer ' + this.accessToken);
  const path = `{"path": "${id}"}`;
  headers.append('Dropbox-API-Arg', path);
  return this.http.post('https://content.dropboxapi.com/2/files/download', 
  null, { headers: headers });
}

主要组件.ts

downloadFileBlob(data: any, name) {
  const blob = new Blob([data], { type: 'image/jpeg' });
  const url = window.URL.createObjectURL(blob);
  window.open(url);
}

saveFile(id, name) {
  this.dropbox.downloadFile(id).subscribe((data: any) => {
  this.downloadFileBlob(data._body, name); });
}

标签: javascriptangulardropbox-api

解决方案


原来我走错了路。
Dropbox github 有一个使用sharingGetSharedLinkFile 的示例,其工作方式与filesDownload 大致相同。
将 sharedGetSharedLinkFile 替换为 filesDownload 并提供文件路径而不是 URL。

像这样的东西:

function downloadFile() {
  var ACCESS_TOKEN = (<HTMLInputElement> document.getElementById('access- 
  token')).value;
  var SHARED_LINK = (<HTMLInputElement> document.getElementById('shared- 
  link')).value;
  var dbx = new Dropbox.Dropbox({ accessToken: ACCESS_TOKEN });
  dbx.filesDownload({path: SHARED_LINK})
 .then(function(data) {
  var downloadUrl = URL.createObjectURL((<any> data).fileBlob);
  var downloadButton = document.createElement('a');
  downloadButton.setAttribute('href', downloadUrl);
  downloadButton.setAttribute('download', data.name);
  downloadButton.setAttribute('class', 'button');
  downloadButton.innerText = 'Download: ' + data.name;
  document.getElementById('results').appendChild(downloadButton);
});
}

推荐阅读