2011-05-28 78 views
0
image = PortfolioFileItem.find(107) 
img_source = "http://s3.amazonaws.com/test/portfolio_file_items_final/original/1.jpg" 
image.picture_from_url(img_source) 
image.save(false) 

圖像保存完成但缺少圖像的擴展名。這是保存的示例圖像名稱: open-uri20110528-6779-fpiust-0。將圖像保存在回形針上時缺少擴展名

請幫我解決問題。感謝

+0

請告訴我這裏的問題之前增加嗎?你能解釋更多嗎?你想要名稱與輸入名稱相匹配嗎? – 2011-05-28 02:19:46

+0

沒有。在這裏我需要回形針保存圖像類型。例如:1.jpg。但它保存了缺失類型(jpg) – khanh 2011-05-28 02:32:32

+0

哦,你想它有一個.jpg擴展名? – 2011-05-28 02:38:32

回答

4

要添加擴展到回形針has_attached_file後添加此行作爲一個選項

:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension" 

您可以自定義這個路徑,以滿足您的需求,但是你必須有.:extension在年底,:擴展是一個可以用於插值的許多值。

查看此blog post瞭解更多信息。

0

如果實際的文件不具有擴展名原本可以檢測擴展和保存

def besfore_save 
    tempfile = data.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.data.instance_write :file_name, "#{tempfile.original_filename}#{ext}" 
     end 
    end 

    true 
    end