javascript - 从 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); });
}
解决方案
原来我走错了路。
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);
});
}
推荐阅读
- c# - 为什么复杂的对象验证会这样工作,而简单的参数却不行?
- unit-testing - 测试在 jenkins 共享库中调用插件函数
- angularjs - 带有angularjs的松弛响应轮播
- sql-server - SQL Server 中的 DELETE 操作极慢
- c# - 正确地将 Serilog 注入 .net 核心类作为 Microsoft.Extentions.Logging.ILogger - 不是 ASP .Net Core
- javascript - 尽管选择的“用户名”已经存在,但用户在注册后被添加到 mongoDB 数据库
- c++ - 访问我以前可以访问的功能时出现分段错误
- performance - 多线程的 JMeter 仪表板报告
- c# - 我们如何使用“yield return”从 MediatR 请求处理程序返回 IAsyncEnumerable?
- sql - 是否可以使用链接表中的值更新本地表?