Ruby on rails 保存图像/八位字节流,base64通过Rails编码为图像文件
我正在使用库将google graph保存为图像。有关此过程的更多信息,请访问此网站 好的,JS框架为我提供了图像的编码数据,我使用表单和隐藏字段发布这些数据,触发下载 HTML代码Ruby on rails 保存图像/八位字节流,base64通过Rails编码为图像文件,ruby-on-rails,image-processing,canvg,Ruby On Rails,Image Processing,Canvg,我正在使用库将google graph保存为图像。有关此过程的更多信息,请访问此网站 好的,JS框架为我提供了图像的编码数据,我使用表单和隐藏字段发布这些数据,触发下载 HTML代码 <form accept-charset="UTF-8" action="<%= rest_png_path %>" id="png_hidden" method="post"> <input id="image_data_input" name="image_data" type
<form accept-charset="UTF-8" action="<%= rest_png_path %>" id="png_hidden" method="post">
<input id="image_data_input" name="image_data" type="hidden" value="" />
<input id="graph_container_div" type="hidden" value="gauge_div" />
<input class="btn " name="commit" type="submit" value="Grafik" />
</form>
最后是响应POST请求和触发下载的控制器代码
def deliver_png
#send_data ActiveSupport::Base64.decode64(params['image_data']),
send_data params['image_data'],
#:type =>'image/png',
:type =>'image/png',
:disposition => "attachment; filename=graf.png"
end
正如你所看到的,我很少使用头球选项。问题是下载的图像已损坏。我通过HexEditor打开了它,我可以看到它没有PNG头,但我对此一无所知。希望您能提供帮助。假设页面正在向服务器提交正确的数据,控制器代码应如下所示:
params['image_data'] =~ /^data:(\w+\/\w+);base64,(.*)/
mime_type= $1
img_base_64= $2
send_data ActiveSupport::Base64.decode64(img_base_64),
:type =>'image/png',
:disposition => "attachment; filename=graf.png"
您基本上需要放弃包含mime类型和字符串“base64”的初始部分
在获取Javascript中的base64数据时,需要检查是否存在其他错误。请通过将隐藏字段更改为文本字段来检查它是否获得了正确的数据
顺便说一句,我还转换为使用jQuery,这使它更易于维护。如果您对jQuery感到满意,请查看并使用此版本。我知道原因(可能)。但我不知道答案。我正在为同样的问题而挣扎。问题是canvg函数的返回是一个完整的img元素,而不仅仅是图片数据。表单不应该有
enctype=“multipart/form data”
No它不需要是enctype=“multipart/form data”,数据作为文本字段值发送,而不是作为文件上传。
params['image_data'] =~ /^data:(\w+\/\w+);base64,(.*)/
mime_type= $1
img_base_64= $2
send_data ActiveSupport::Base64.decode64(img_base_64),
:type =>'image/png',
:disposition => "attachment; filename=graf.png"