Javascript 如果我只有PDF URL,如何将PDF作为附件发送
我有一个ionic应用程序,它根据用户的输入进行某些计算。计算后,通过API(generatePDF)调用将结果转换为PDF。另一个要求是通过电子邮件发送相同的PDF。API(sendMail)是为同样的目的而制作的enctype='multipart/form data'设置在sendMail API的头部分 我现在有了PDF URL,它是GeneratePDFAPI的响应。使用此URL如何将PDF附加到我要发送的邮件 有人能帮忙吗?谢谢@MissakBoyajian的帮助。以下是我所做的 已安装ionic native的文件传输和文件插件Javascript 如果我只有PDF URL,如何将PDF作为附件发送,javascript,ionic-framework,email-attachments,Javascript,Ionic Framework,Email Attachments,我有一个ionic应用程序,它根据用户的输入进行某些计算。计算后,通过API(generatePDF)调用将结果转换为PDF。另一个要求是通过电子邮件发送相同的PDF。API(sendMail)是为同样的目的而制作的enctype='multipart/form data'设置在sendMail API的头部分 我现在有了PDF URL,它是GeneratePDFAPI的响应。使用此URL如何将PDF附加到我要发送的邮件 有人能帮忙吗?谢谢@MissakBoyajian的帮助。以下是我所做的 已
this.platform.ready().then(() => {
const fileTransfer: FileTransferObject = this.transfer.create();
const pdfLocation = this.pdffile;//pdffile is the PDF URL which we got as the response from the generatePDF API
fileTransfer.download(pdfLocation, this.storageDirectory + "filename.pdf").then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `PDF was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
this.file.readAsDataURL(this.storageDirectory, 'filename.pdf')
.then((datafile) =>{
this.attachpdf(id,datafile);
})
.catch((err) =>{
console.log("Error is that "+err);
});
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `PDF was not successfully downloaded. Error code: ${error.code}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
使用一个函数(我从搜索中获得)将Base64数据转换为blob
public dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
它终于成功了。您必须使用下载文件,然后将下载的文件发送到您的API@MissakBoyajian已安装本机文件传输插件。添加了
constfiletransfer:FileTransferObject=this.transfer.create();const url=\u指向pdf的url;fileTransfer.download(url,this.file.dataDirectory+''.\u filename..pdf')。然后((条目)=>{alert(this.file.dataDirectory);},(error)=>{alert(error);//handle error})代码>警报给出的路径为file:///data/user/0/io.ionic.starter/files/,如果选中,则不会下载此类文件。这是临时下载文件的正确方法吗?请尝试使用@MissakBoyajian@MissakBoyajian位置读取该文件。抱歉,延迟回复。使用上述插件读取该文件。this.file.readAsText(this.storageDirectory,'filename.pdf')
是使用的代码行。然后将获取的文件传递给API(sendMail)。该邮件现随附PDF(大小为60KB)发送。但当打开时,里面没有内容,这意味着它是一个空白的PDF。原因可能是什么?请帮忙。很高兴我能帮忙:)
public attachpdf(emailid,filetoattach){
let headers = new Headers();
headers.append(...);
headers.append('enctype','multipart/form-data');
var blob = this.dataURItoBlob(filetoattach);
var data ={... };
var formData = new FormData();
formData.append("data",JSON.stringify(data));
formData.append("doc",blob);
this.http.post('sendMail API',formData, {headers: headers})
.map(res => res.json())
.subscribe(results => {
...
},
error=>{
..
}
)
}