我是使用Ionic与角和制作一个应用程序。在我的应用程序中,我使用长沙发数据库作为下载映像。
我用的是Android手机。
我正试图在文件打开器和的帮助下下载我的文件,我得到了 ->
(状态:9消息:文件未找到)
<ion-item mode=md class="input-group ion-no-padding viewFile" lines=none *ngIf="common.isDocumentFill">
<span (click)="doc(fc)">View File</span>
</ion-item>//职能
doc(objFC) {
let obj = {
ServiceId: 1,
CouchDBDocId: objFC.CouchDBDocId,
DocumentName: objFC.FileName
}
this.api.getPdf(obj).subscribe((data: any) => {
// debugger
let blob = new Blob([data], { type: data.type });
var downloadURL = window.URL.createObjectURL(data);
var types = data.type
this.down(downloadURL, types)
});
}
down(filepath, mimeType) {
this.fileOpener.open(filepath, mimeType)
.then(() =>
console.log('File is opened')
)
.catch(e => console.log('Error opening file', e));
}和服务
getPdf(obj) {
// debugger
const httpOptions = {
responseType: 'blob' as 'json'
};
return this.http.post(this.url + "DocumentDetails/DownloadFile", obj, httpOptions);
} 发布于 2021-06-10 12:29:28
请安装所需的插件
1.import { FileOpener } from '@ionic-native/file-opener/ngx';
2.import { File } from '@ionic-native/file/ngx';在构造函数中实例化
constructor(
private fileOpener: FileOpener,
private file: File
)将此函数放入ts文件中。
openPDF (stringBase64PDF) {
debugger
fetch('data:application/pdf;base64,' + stringBase64PDF, {
method: "GET"
})
.then(res => res.blob()).then(blob => {
console.log("created blob");
this.file.createFile(this.file.dataDirectory, 'temp.pdf', true)
.then(() => {
console.log("file created");
this.file.writeFile(this.file.dataDirectory, 'temp.pdf', blob, { replace: true })
.then(res => {
console.log("file writed");
this.fileOpener.open(res.toInternalURL(), 'application/pdf')
.then((res) => {
console.log('file opened')
}).catch(err => {
console.log('open error')
});
}).catch(err => {
console.log('write error')
});
}).catch(() => {
console.log("create error");
})
}).catch(err => {
console.log('blob error')
});
}放入html文件
<ion-item (click)="openPDF(base64pdf)">
<ion-label>Click here to open pdf file</ion-label>
</ion-item> 发布于 2021-06-02 13:09:18
尝试创建URL,如
var downloadURL = (window.webkitURL || window.URL).createObjectURL(data);浏览器可能不支持window.URL -> read here。
你还指的是iOS设备或安卓在谈论手机吗?
https://stackoverflow.com/questions/67802064
复制相似问题