Jquery 保存图像时ruby中的IOError(封闭流)
在这里,我试图使用ajax将图像保存到mysql数据库中,但每个 图片上传失败,请帮我解决 Controller.rbJquery 保存图像时ruby中的IOError(封闭流),jquery,ruby,ajax,Jquery,Ruby,Ajax,在这里,我试图使用ajax将图像保存到mysql数据库中,但每个 图片上传失败,请帮我解决 Controller.rb def image_converter sub = TestimonialPicture.new decoded_file = Base64.decode64(params[:picdata]) unless params[:picdata].nil? if request.post? begin
def image_converter
sub = TestimonialPicture.new
decoded_file = Base64.decode64(params[:picdata]) unless params[:picdata].nil?
if request.post?
begin
tmpfilename = "#{params[:id]}#{Time.now.to_i.to_s}"
file = Tempfile.new([tmpfilename, '.jpg'])
file.binmode
file.write decoded_file
file.close
sub.testimonialpic = file
if sub.save
sub.update_attributes(:testimonial_id => params[:tid])
msg = "YOur image has been successfully uploaded"
else
msg = "Failed to create submission. Please try again"
end
ensure
file.unlink
end
render :text=>msg
end
end
我创建的图像我将把它转换成画布
和保存,通过点击保存按钮,两个我正在做的功能,使用它
控制器我可以获取图像,但无法存储到
数据库
view.html.erb
每次我收到一个错误,比如IOError(封闭流)
我需要通过单击“保存”按钮将图片保存到mysqldatabase中
有问题的线路是
file.close
sub.testimonialpic = file
当mysql2
试图将IO
保存到BLOB列时,该IO
必须打开才能读取。在保存该文件之前,您只需显式关闭该文件(它是一个IO)
解决办法是
file.rewind
sub.testimonialpic = file
然后在
块中关闭该文件,确保在其取消链接之前将其关闭。MissingRequiredValidatorError(回形针::错误::MissingRequiredValidatorError):在添加了同样的错误后,请向我展示您的模型类。它对我来说很好。保存图像后,请告诉我,新窗口应该使用ajax方法关闭。我在ajax方法$(“#btn-Convert-Html2Image”)。on('click',function(){$(“#popDiv”)。hide();var picdata=getCanvas.toDataURL(“image/png”);var pic=picdata.replace(/^data:image\/(png|jpg);base64,/,”;var tid=$(“#tid”).val();$.ajax({url:/businessdb/image_converter),类型:“POST”,数据:{pic pic,“tid”:tid},数据类型:“json”,成功:函数(数据){window.open(window.location,“_self”).close();}}});
file.rewind
sub.testimonialpic = file