【问题标题】:How can i send a PDF as an attachment , if I only have the PDF URL如果我只有 PDF URL,如何将 PDF 作为附件发送
【发布时间】:2018-06-18 15:04:18
【问题描述】:

我有一个离子应用程序,它根据用户的输入进行某些计算。计算后,结果通过 API(generatePDF) 调用转换为 PDF。另一个要求是通过电子邮件发送相同的 PDF。一个 API(sendMail) 就是为此而生的。 enctype='multipart/form-data' 设置在 sendMail API 的标头部分。

我现在有了 PDF URL,它是作为 generatePDF API 的响应而得到的。如何使用此 URL 将 PDF 附加到我打算发送的邮件中?

有人可以帮忙吗?

【问题讨论】:

  • 您必须使用ionicframework.com/docs/native/file-transfer 下载文件然后将下载的文件发送到您的API
  • @MissakBoyajian 安装了本机文件传输插件。添加了const fileTransfer: FileTransferObject = this.transfer.create(); const url = _url to pdf_; fileTransfer.download(url, this.file.dataDirectory + '_filename_.pdf').then((entry) => { alert(this.file.dataDirectory); }, (error) => { alert(error); // handle error }); 警报给出的路径为 file:///data/user/0/io.ionic.starter/files/,当检查时没有下载这样的文件。这是临时下载文件的正确方法吗?
  • 尝试读取该位置的文件ionicframework.com/docs/native/file
  • @MissakBoyajian 抱歉回复晚了。使用上述插件读取文件。this.file.readAsText(this.storageDirectory, 'filename.pdf') 是使用的代码行。然后将获取的文件传递给 API(sendMail)。现在发送的邮件附有 PDF(60KB 大小)。但是打开后,里面没有内容,这意味着它是一个空白的PDF。可能是什么原因?请帮忙。
  • 你有空pdf文件的解决方案吗?

标签: javascript ionic-framework email-attachments


【解决方案1】:

感谢@MissakBoyajian 的帮助。这就是我所做的。

为 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();
      });
    });

使用一个函数(我从搜索中得到)将 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=>{
      ..
    }
    )
  }

它终于奏效了。

【讨论】:

  • 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
相关资源
最近更新 更多