Ruby on rails 回形针:保存Base64编码文件,无扩展名保存
我试图使用回形针在rails上保存Base64编码的字符串,但生成的文件没有扩展名 编码字符串如下所示:Ruby on rails 回形针:保存Base64编码文件,无扩展名保存,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我试图使用回形针在rails上保存Base64编码的字符串,但生成的文件没有扩展名 编码字符串如下所示: {"model"=>{"photo"=>"data:image/jpeg;base64,/9j..} 回形针配置 has_attached_file :photo, :styles => { medium: "380x380>", small: "200x200>", thumb: "100x100>" },
{"model"=>{"photo"=>"data:image/jpeg;base64,/9j..}
回形针配置
has_attached_file :photo,
:styles => { medium: "380x380>", small: "200x200>", thumb: "100x100>" },
:path => ":rails_root/model/:style/:id.:extension",
:url => "/object_image/model/:style/:id.:extension",
:default_url => "/images/default-avatar.png"
validates_attachment_content_type :photo,
:content_type => ["image/jpg", "image/jpeg",
"image/png", "image/gif"]
结果文件是:1。
任何帮助都是徒劳的。
谢谢尝试使用图像的内容类型设置扩展
tempfile = self.image.queued_for_write[:original]
unless tempfile.nil?
extension = File.extname(tempfile.original_filename)
if !extension || extension == ''
mime = tempfile.content_type
ext = Rack::Mime::MIME_TYPES.invert[mime]
self.image.instance_write :file_name, "#{tempfile.original_filename}#{ext}"
end
end
特别感谢stackoverflow。尝试使用图像的内容类型设置扩展
tempfile = self.image.queued_for_write[:original]
unless tempfile.nil?
extension = File.extname(tempfile.original_filename)
if !extension || extension == ''
mime = tempfile.content_type
ext = Rack::Mime::MIME_TYPES.invert[mime]
self.image.instance_write :file_name, "#{tempfile.original_filename}#{ext}"
end
end
特别感谢stackoverflow。我也遇到了同样的问题,但我真的不在乎文件是否有扩展名,因为我正在将其上载到s3。为了做其他事情,我需要获得文件扩展名。这是我上传后从base64编码文件中获取文件扩展名的解决方案 在model.rb中
file_path = attachment.queued_for_write[:original].path
# tmp/filename_without_extension
ext = '.' + attachment.queued_for_write[:original].content_type.downcase.split('/').last
# .content_type returns something like "image/png"```
我也遇到了同样的问题,但我真的不在乎文件是否有扩展名,因为我正在将它上传到s3。为了做其他事情,我需要获得文件扩展名。这是我上传后从base64编码文件中获取文件扩展名的解决方案 在model.rb中
file_path = attachment.queued_for_write[:original].path
# tmp/filename_without_extension
ext = '.' + attachment.queued_for_write[:original].content_type.downcase.split('/').last
# .content_type returns something like "image/png"```