ruby-on-rails – 使用Rails在PaperClip中上传Base64编码的字符
发布时间:2020-12-16 19:19:50 所属栏目:百科 来源:网络整理
导读:我有一个base64编码的图像文件的字符串.我需要使用Paper Clip保存它 我的控制器代码是 @driver = User.find(6) encoded_file = Base64.encode64(File.open('/pjt_path/public/test.jpg').read) decoded_file = Base64.decode64(encoded_file) @driver.profil
我有一个base64编码的图像文件的字符串.我需要使用Paper Clip保存它
我的控制器代码是 @driver = User.find(6) encoded_file = Base64.encode64(File.open('/pjt_path/public/test.jpg').read) decoded_file = Base64.decode64(encoded_file) @driver.profile_pic = StringIO.open(decoded_file) @driver.save 在我的用户模型中 has_attached_file :profile_pic,:styles => { :medium => "300x300>",:thumb => "100x100>" },:default_url => '/icon.jpg' 目前,该文件保存为文本文件(stringio.txt).但是当我将扩展名更改为JPG时,我可以将其视为图像.如何使用StringIO正确命名图像. 我有rails 3.2,ruby 1.9.2,paperclip 3.0.3 解决方法
我通过使用修复了这个问题
encoded_file = Base64.encode64(File.open('/pjt_path/public/test.jpg').read) decoded_file = Base64.decode64(params[:encoded_image]) begin file = Tempfile.new(['test','.jpg']) file.binmode file.write decoded_file file.close @user.profile_pic = file if @user.save render :json => {:message => "Successfully uploaded the profile picture."} else render :json => {:message => "Failed to upload image"} end ensure file.unlink end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |