简体   繁体   English

如果我只有PDF URL,如何发送PDF作为附件

[英]How can i send a PDF as an attachment , if I only have the PDF URL

I have an ionic application, which do certain calculations based on user's inputs.After the calculations, the results are getting converted into PDF via an API(generatePDF) call.Another requirement is to email the same PDF. 我有一个离子应用程序,该应用程序根据用户的输入进行某些计算。计算后,结果通过API(generatePDF)调用转换为PDF。另一个要求是通过电子邮件发送相同的PDF。 An API(sendMail) is made for the same. 一个API(sendMail)是为相同的。 enctype='multipart/form-data' is set in the header part of the sendMail API. 在sendMail API的标头部分设置了enctype ='multipart / form-data'

I now have the PDF URL, which i get as the response of generatePDF API. 我现在有了PDF URL,它是generatePDF API的响应。 Using this URL how can i attach the PDF to the mail that I am intended to send? 如何使用该URL将PDF附加到要发送的邮件上?

Could anyone please help? 谁能帮忙吗?

Thank you @MissakBoyajian for the help.Here is what i did. 谢谢@MissakBoyajian的帮助。这就是我所做的。

Installed File Transfer and File plugin for 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();
      });
    });

Used a function(which i got from searching) to convert base64data into blob. 使用一个函数(我从搜索中获得)将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});
 }

Then, 然后,

    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=>{
      ..
    }
    )
  }

And it finally worked. 终于奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM