Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Rails文件上载回形针中的Base64编码字符串_Ruby On Rails_Ruby_File Upload_Paperclip_Stringio - Fatal编程技术网

Ruby on rails 使用Rails文件上载回形针中的Base64编码字符串

Ruby on rails 使用Rails文件上载回形针中的Base64编码字符串,ruby-on-rails,ruby,file-upload,paperclip,stringio,Ruby On Rails,Ruby,File Upload,Paperclip,Stringio,我有一个图像文件的base64编码字符串。我需要用回形针保存它 我的控制器代码是 @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.

我有一个图像文件的base64编码字符串。我需要用回形针保存它

我的控制器代码是

 @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

尝试设置
路径
/
:url
选项已附加文件
,并显式覆盖扩展名:

分别


我通过使用

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

你能多发一些你的代码吗?我觉得很有趣@EmSta-我已经在这里发布了完整的代码。此代码添加到我的控制器函数中。请让我知道你还需要什么代码我提出了一个新问题:也许你能帮助我@AmalKumarS@EmSta-它显示“这个问题是作者自愿删除的。”在这个版本上,我如何处理多个扩展?