我有一个 Base64 编码的 PDF 数据,想使用 ActionMailer 将其设置为邮件的附件。
我尝试过如下(假设 Base64 编码的 pdf 数据在 base64_encoded_string
中):
attachments['attachment.pdf'] = {
mime_type: 'application/pdf',
encoding: 'base64',
content: base64_encoded_string
}
但是当我在收到的电子邮件上打开附加的 pdf 文件时,该文件已损坏。
现在我提前解码了一个 Base64 字符串,让 ActionMailer 对 Base64 进行编码,它的工作没有任何问题。
attachments[File.basename('attachment.pdf')] = Base64.decode64(base64_encoded_string)
如何将 Base64 编码的字符串直接设置为 pdf 附件?
最佳答案
似乎我找到了解决方案。如果您提供 base64 字符串作为内容,请确保指定编码。否则 Rails 将第二次编码已经编码的字符串。
attachments['base64_file.pdf'] = {
mime_type: 'application/pdf',
encoding: 'base64',
content: base64_string
}
关于ruby-on-rails - 在 Ruby on Rails 中,如何将 Base64 编码的字符串设置为 pdf 附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451025/