0
我使用RubyZip壓縮一組圖像(使用Paperclip上傳)並允許用戶將它們下載到一個文件中,並且所有工作都正常,直到我來到打開圖像。 它不會顯示,並試圖在Ubuntu我得到的錯誤信息:RubyZip將JPG轉換爲「不是JPG,以0x89開頭」
"Error interpreting JPEG image file (Not a JPEG file: starts with 0x89..."
所以用戶正在下載的文件夾,用正確的用戶名文件填充,但一旦打開無法顯示,因爲計算機可以」 t顯示他們的「格式」。
控制器:
def zip
@product = Product.find(params[:id])
t = Tempfile.new(@product.random+rand(200).to_s)
Zip::ZipOutputStream.open(t.path) do |z|
@product.assets.each do |img|
img_path = "#{RAILS_ROOT}"+"/public"+img.data.url(:original)
file = File.open(img_path.split('?')[0])
z.put_next_entry(img.id.to_s+"_original.jpg")
z.print IO.read(file.path)
end
end
send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "#{@product.random}-#{rand(9999).to_s}.zip"
end
謝謝!
我不確定它是否是一個0x89(我認爲是),但錯誤信息正在被切斷。 上傳的文件是JPG格式,但在控制器代碼中將文件擴展名更改爲PNG會給出一個新的錯誤消息:「通過ASCII轉換損壞的PNG文件」... – wastedhours 2010-07-21 11:38:59
查看服務器上文件的結果是什麼? – 2010-07-21 11:43:15
不,不要緊,服務器上的文件可能沒問題。您需要打開文件以二進制模式閱讀,否則會損壞它。 – 2010-07-21 11:47:53