[英]How can i send a PDF as an attachment , if I only have the PDF URL
我有一個離子應用程序,該應用程序根據用戶的輸入進行某些計算。計算后,結果通過API(generatePDF)調用轉換為PDF。另一個要求是通過電子郵件發送相同的PDF。 一個API(sendMail)是為相同的。 在sendMail API的標頭部分設置了enctype ='multipart / form-data' 。
我現在有了PDF URL,它是generatePDF API的響應。 如何使用該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();
});
});
使用一個函數(我從搜索中獲得)將base64data轉換為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});
}
然后,
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=>{
..
}
)
}
終於奏效了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.