Dud*_*dis 3 ruby ruby-on-rails paperclip
我的应用程序中有适用于Paperclip gem的文档(pdf,doc)。我需要通过邮寄请求将文档传递给其他第三方应用程序。
我试图通过Base64转换回形针附件,但会引发错误:没有将Tempfile隐式转换为String
这是我的做法:
# get url from the paperclip file
url = document.doc.url # https://s3-ap-southeast-1.amazonaws.com/xx-eng/documents/xx/000/000/xx/original/doc.pdf
file_data = open(url)
# Encode the bytes to base64 - this line throw error
base_64_file = Base64.encode64(file_data)
Run Code Online (Sandbox Code Playgroud)
您对如何避免Tempfile错误有任何建议?
You need to read file first.
base_64_file = Base64.encode64(file_data.read)
Run Code Online (Sandbox Code Playgroud)
Here is working example:
$ bundle exec rails c
=> file = open("tmp/file.pdf")
#> #<File:tmp/receipts.pdf>
=> base_64 = Base64.encode64(file)
#> TypeError: no implicit conversion of File into String
=> base_64 = Base64.encode64(file.read)
#> "JVBERi0xLjQKMSAwIG9iago8PAovVGl0b/BBQEPgQ ......J0ZgozMDM0OQolJUVPRgo=\n"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2671 次 |
| 最近记录: |