从Dropbox下载的空白图像和pdf

时间:2018-05-28 18:07:41

标签: javascript angular dropbox-api

从我的保管箱下载文件时出现问题 使用类型' text / csv'我可以下载和查看txt文件 将类型转换为' image / jpeg'或者' application / pdf'并下载文件类型给我空白文件。

我是在正确的轨道上还是应该采取另一种方式?

main.service.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 });
}

main.component.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); });
}

1 个答案:

答案 0 :(得分:0)

原来我是以错误的方式行事 Dropbox github有一个使用sharingGetSharedLinkFile的例子,它与filesDownload的工作方式大致相同。
将sharingGetSharedLinkFile替换为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);
});
}