如果我只有 PDF URL,如何将 PDF 作为附件发送
How can i send a PDF as an attachment , if I only have the PDF URL
我有一个离子应用程序,它根据用户的 inputs.After 计算进行某些计算,结果通过 API(generatePDF) call.Another 要求转换为 PDF通过电子邮件发送相同的 PDF。 API(sendMail) 是为此而制作的。 enctype='multipart/form-data'设置在sendMail的头部API。
我现在有 PDF URL,这是我作为 generatePDF API 的响应得到的。使用此 URL 如何将 PDF 附加到我要发送的邮件中?
有人可以帮忙吗?
谢谢@MissakBoyajian help.Here 是我所做的。
为 ionic native 安装文件传输和文件插件
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});
}
然后,
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=>{
..
}
)
}
终于成功了。
我有一个离子应用程序,它根据用户的 inputs.After 计算进行某些计算,结果通过 API(generatePDF) call.Another 要求转换为 PDF通过电子邮件发送相同的 PDF。 API(sendMail) 是为此而制作的。 enctype='multipart/form-data'设置在sendMail的头部API。
我现在有 PDF URL,这是我作为 generatePDF API 的响应得到的。使用此 URL 如何将 PDF 附加到我要发送的邮件中?
有人可以帮忙吗?
谢谢@MissakBoyajian help.Here 是我所做的。
为 ionic native 安装文件传输和文件插件
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});
}
然后,
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=>{
..
}
)
}
终于成功了。