From: Bin Bin Date: 2009-08-10T18:10:48+09:00 Subject: Re: How do I upload an image with Sinatra (like Paperclip)? Brian Candler wrote: > Tony Tony wrote: >> I need to upload an image from my local computer to a web server and >> store the location in a database (similar to how Paperclip and >> attachment_fu work) in Sinatra. > > I don't know Paperclip or attachment_fu, but getting the attachment in > Sinatra is easy: you get an open tempfile that you read from. > > Something like this (extracted from some working code but not tested in > isolation): > > post '/upload' do > unless params[:file] && > (tmpfile = params[:file][:tempfile]) && > (name = params[:file][:filename]) > @error = "No file selected" > return haml(:upload) > end > STDERR.puts "Uploading file, original name #{name.inspect}" > while blk = tmpfile.read(65536) > # here you would write it to its final location > STDERR.puts blk.inspect > end > "Upload complete" > end > > --------- > %h1 Upload > > %form{:action=>"/upload",:method=>"post",:enctype=>"multipart/form-data"} > %input{:type=>"file",:name=>"file"} > %input{:type=>"submit",:value=>"Upload"} That is helpful.Thank u so much --------------- http://dressup9x.com is a best Fashion Games, Girl Games, Dressup Game -- Posted via http://www.ruby-forum.com/.