views:

116

answers:

3

Hello,

what's the best way to download an image and save it?

My current code is:

temp_file = Tempfile.new "filename", "#{RAILS_ROOT}/tmp/"
temp_file.puts open(path_to_picture, 'User-Agent' => 'Test').read 
mimetype = `file -ib #{temp_file.path}`.gsub(/\n/,"")

But it seems, that the mimetyp isn't ok.

Best regards

A: 

You should not use puts, because that writes a newline at the end of the picture.

hrnt
+2  A: 

Take a look at the Rails' send_file method. You also might want to read File Downloads Done Right over at The Rails Way.

John Topley
A: 

I've now used the gem "rio" and it works very well!

brainfck