ruby - How to write an image to filesystem in rails 3.1.1? -
i feel should close, can't work quite right.
i'm sending image using filetransfer phonegap app rails app (3.1.1 ruby 1.8.7 btw).
i want write file filesystem.
this params hash:
parameters: {"file"=>#<actiondispatch::http::uploadedfile:0x7f1e2baa94e8 @tempfile=#<file:/tmp/rackmultipart20120607-5707-owzii5-0>, @headers="content-disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\ncontent-type: image/jpeg\r\n", @content_type="image/jpeg", @original_filename="image.jpg">}
basically, can access file through params[:file]. need write image on filesystem. don't have model image correspond to, don't need paperclip. right now, best come this:
open(params[:file]) |image| file.open("theiamge.jpg","wb"){|file| file.puts image.read } end
this error get:
typeerror (can't convert actiondispatch::http::uploadedfile string):
edit:
so, rather odd. can these commands working in irb:
file.open('newimage.jpg', 'wb') |f| f.write file.open('oldimage.jpg').read end
however, doesn't work:
file.open("ihatedetails.jpg",'wb') |f| f.write file.open(params[:file]).read end
i error: typeerror (can't convert actiondispatch::http::uploadedfile string):
i'm using ruby 1.9.3 in irb, these commands should same...
so i'm stumped right now.
the code should be:
file.open("ihatedetails.jpg",'wb') |f| f.write params[:file].read end
or file.open("ihatedetails.jpg",'wb') { |f| f.write params[:file].read }
in 1 line.
Comments
Post a Comment