我在我的 Angular 6 应用程序中使用服务从我的 REST API 下载文件(blob)。此服务适用于小于 10MB 的文件(在 Chrome 和其他浏览器上)。我目前正在尝试下载一个 14MB 的文件,但它失败了,仅在 Chrome 上(按预期与 Firefox 一起使用)
我认为这是 Chrome 的限制,但我在 SO 或 Angular 文档上找不到任何关于此的信息。
这是我的服务
displayFile(url: string) {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
this.http.post('my_backend_path/getFile', JSON.stringify({urlData: url}), {responseType: "blob", headers})
.subscribe((data: any) => {
var urlBlob = window.URL.createObjectURL(data);
window.open(urlBlob);
},
(error) => {
//Here comes the error
});
}
Run Code Online (Sandbox Code Playgroud)
我的节点 REST API 以 200 状态正确返回文件。失败来自 Chrome 控制台,如下所示:
POST my_backend_path/getFile net::ERR_FAILED 200 (OK)
Run Code Online (Sandbox Code Playgroud)
在 chrome 的网络调试器中,我可以看到文件大小估计为 10.0MB(而文件实际上是 14MB)。这就是为什么我认为这是由于某些 Chrome …
我正在使用 Angular 9 / Ionic 5 / Capacitor 2.0 开发混合应用程序。我正在尝试将来自 API 的文件(基本上是 PDF 或图像文件)作为 Blob 保存到我的移动设备(IOS/Android)的文件系统中。我的最终目标是重现文件的类似浏览器的下载功能(允许用户将其保留在手机上)。
这是我的代码:
downloadNativeFile(blob: Blob, fileName: string){
let reader = this.getFileReader();
reader.onloadend = (readerEvent) => {
if(reader.error){
console.log(reader.error);
} else {
let base64data: any = readerEvent.target['result'];
try {
Filesystem.writeFile({
path: fileName,
data: base64data,
directory: FilesystemDirectory.Data
}).then((res)=>{
Filesystem.getUri({
directory: FilesystemDirectory.Data,
path: fileName
}).then((getUriResult) => {
const path = getUriResult.uri;
console.log("The file's path : " + path);
console.log(Capacitor.convertFileSrc(getUriResult.uri));
}, (error) => {
console.log(error);
});
}).catch((err)=>{
console.log("Error", err);
}); …Run Code Online (Sandbox Code Playgroud)