【发布时间】:2017-06-22 20:28:36
【问题描述】:
我的应用程序中有一个 Base64 编码的图像。我想在其他地方重新发布该图像,但它在目的地将内容类型设置为 multipart/form-data。我如何上传这张图片?
file_name = permitted_params[:file_name]
file_contents = permitted_params[:file_contents]
file = Tempfile.new( file_name )
file.binmode
file.write( Base64.decode64( file_contents ) )
file.rewind()
raw_response = RestClient.put(
url,
{ 'upload' => file, :content_type => 'image/jpeg' },
:headers => {:content_type => 'image/jpeg'}
)
更新(已解决)
我需要使用 RestClient,因为我需要将它传递到另一台服务器(因此 PUT 中的“url”)。
我的问题是在解码图像时我没有去掉
data:image/jpeg;base64,
然后使用此代码:
raw_response = RestClient.put(url,
file_binary,
{:content_type => imageContentType})
我能够让它放置图像并设置正确的内容类型。不过,下面的答案确实有所帮助,因为我尝试了它以确保图像被正确解码,但事实并非如此。
【问题讨论】:
标签: ruby-on-rails