我试图在rails上使用paperclip保存Base64编码的字符串,但生成的文件没有扩展名
编码的字符串如下:
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.
生成的文件为:{{1}}
任何帮助都是预先确定的。 谢谢
答案 0 :(得分:2)
尝试使用图片的内容类型
来回退扩展程序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。
答案 1 :(得分:0)
我遇到了同样的问题,但是我真的不在乎文件是否具有扩展名,因为我正在将其上传到s3。为了做其他一些事情,我需要获取文件扩展名。这是我上传后从base64编码文件获取文件扩展名的解决方案。
在模型中。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"```