我是离子框架的新手,实际上我将字节数组作为后端服务的响应并将其转换为blob。如何将其保存到离子中的PDF?
var blob = new Blob([res], { type: 'application/pdf' });
此处是服务中的响应(字节数组)。
代码
var blob = new Blob([res], { type: 'application/pdf' });
let fileName="Receipt.pdf";
let filePath = (this.platform.is('android')) ?
this.file.externalRootDirectory : this.file.cacheDirectory;
this.file.writeFile(filePath, fileName, blob, { replace: true }).then((fileEntry) => {
console.log("File created!");
this.fileOpener.open(fileEntry.toURL(), 'application/pdf')
.then(() => console.log('File is opened'))
.catch(err => console.error('Error openening file: ' + err));
})
.catch((err) => {
console.error("Error creating file: " + err);
throw err;
});
预先感谢。
为了保存PDF,您需要使用一些Cordova插件。Ionic在这里周围有一些不错的包装纸。查看文件,文件传输和文件开启器插件。
以下是一些示例代码,一旦将这些插件包含在项目中:
var blob = new Blob([res], { type: 'application/pdf' });
//Determine a native file path to save to
let filePath = (this.appConfig.isNativeAndroid) ? this.file.externalRootDirectory : this.file.cacheDirectory;
//Write the file
this.file.writeFile(filePath, fileName, blob, { replace: true }).then((fileEntry: FileEntry) => {
console.log("File created!");
//Open with File Opener plugin
this.fileOpener.open(fileEntry.toURL(), data.type)
.then(() => console.log('File is opened'))
.catch(err => console.error('Error openening file: ' + err));
})
.catch((err) => {
console.error("Error creating file: " + err);
throw err; //Rethrow - will be caught by caller
});