[英]Ionic 2 File Storage
I am currently trying to write a PDF that was created using PDFMake to file, and then take that file and attach it to an email. 我目前正在尝试将使用PDFMake创建的PDF写入文件,然后获取该文件并将其附加到电子邮件中。 I have spent 6 hours trying to do this and I keep getting errors.
我花了6个小时来尝试执行此操作,并且不断收到错误消息。
Below is my code for exporting the PDF. 下面是我导出PDF的代码。 I tested the PDF in serve using createPDF.open() and it worked.
我使用createPDF.open()在服务中测试了PDF,并且可以正常工作。 I have also printed the Base64 data in the console.
我还在控制台中打印了Base64数据。
exportPDF() {
console.log("ExportPDF()")
this.pdf = pdfMake;
// For use in browser(web)
//this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).open();
let data : string;
let newPDF;
this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).getBase64((buffer) => {
data = buffer;
console.log("newPDF: " + data)
});
// this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).getBuffer((buffer) => {
// data = buffer.toArrayBuffer();
// console.log("newPDF: " + data)
// });
// Platforms
if (this.plat.is('ios')) {
console.log("Platform: ios")
let file = "testFingering.pdf"
console.log(cordova.file.cacheDirectory);
File.removeFile(cordova.file.cacheDirectory, file);
File.writeFile(cordova.file.cacheDirectory, file, data).then(() => {
// Success!
console.log("writeFile: success!")
SocialSharing.share("tits", "tits", cordova.file.cacheDirectory + file).then(() => {
// Success!
console.log("shareViaEmail: success!")
}).catch((err) => {
// Error!
console.log("shareViaEmail: fail! " + err)
});
}).catch((err) => {
// Error!
console.log("writeFile: fail! " + JSON.stringify(err));
});
} else if (this.plat.is('android')) {
console.log("Platform: android")
} else if (this.plat.is('windows')) {
console.log("Platform: windows")
} else {
console.log("Platform: web")
}
}
Here are the errors I am receiving: 这是我收到的错误:
2017-01-31 20:40:12.997343 CMTA[1725:417453] writeFile: fail! {"code":12,"message":"PATH_EXISTS_ERR"}
2017-01-31 20:40:13.013456 CMTA[1725:417453] writeFile: fail! {"code":"Invalid parameter type"}
I am only executing this code once, so 1) I am not sure why it is logging two errors. 我只执行一次此代码,因此1)我不确定为什么会记录两个错误。
2) I know "PATH_EXISTS_ERR" means the path already exists, but if you look in the code, I am deleting the file and recreating it every time. 2)我知道“ PATH_EXISTS_ERR”表示该路径已经存在,但是如果您在代码中查找,我将每次删除该文件并重新创建它。
3) What parameter should I be inputing? 3)我应该输入什么参数? It says it will accept "text", should I be using something else?
它说它将接受“文本”,我是否应该使用其他内容? I tried toBufferArray() before because that's what I did in my Ionic 1 version of this app, but that isn't working anymore.
我之前尝试过toBufferArray(),因为那是我在该应用程序的Ionic 1版本中所做的,但是现在不起作用了。
I would greatly appreciate any help as this is frustrating me to no end. 我非常感谢您的帮助,因为这无休止地困扰着我。
Well I ended up getting everything. 好吧,我最终得到了一切。 I had to do two steps though.
我不得不做两个步骤。
1) I had to move the File.writeFile() into the response from my createPDF() call. 1)我必须将File.writeFile()移到createPDF()调用的响应中。
2) I had to getBuffer() instead of getBase64() and then found a function on the internet for toBufferArray(). 2)我必须使用getBuffer()而不是getBase64(),然后在互联网上为toBufferArray()找到了一个函数。
Here is the final code: 这是最终代码:
exportPDF() {
console.log("ExportPDF()")
this.pdf = pdfMake;
// For use in browser(web)
//this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).open();
let data;
let file = "test11.pdf";
let newPDF;
this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).getBuffer((buffer) => {
data = this.toArrayBuffer(buffer);
console.log("newPDF: " + data);
console.log(cordova.file.cacheDirectory + file);
File.removeFile(cordova.file.cacheDirectory, file);
File.writeFile(cordova.file.cacheDirectory, file, data, true).then(() => {
// Success!
console.log("writeFile: success!");
SocialSharing.share("tits", "tits", cordova.file.cacheDirectory + file).then(() => {
// Success!
console.log("shareViaEmail: success!")
}).catch((err) => {
// Error!
console.log("shareViaEmail: fail! " + err)
});
}).catch((err) => {
// Error!
console.log("writeFile: fail! " + JSON.stringify(err));
});
});
// this.pdf.createPdf(getDocDefinition(this.issues, this.project, this.report)).getBuffer((buffer) => {
// data = buffer.toArrayBuffer();
// console.log("newPDF: " + data)
// });
}
toArrayBuffer(buf) {
let ab = new ArrayBuffer(buf.length);
let view = new Uint8Array(ab);
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.