Ruby Forum Ruby > How do I upload an image with Sinatra (like Paperclip)?

Posted by Tony Tony (slythic)
on 09.08.2009 01:02
Hi all,

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.

Any ideas on how to get this to work?


Thanks in advance,
Tony
Posted by Marcos Vanetta (malev05)
on 09.08.2009 01:42
(Received via mailing list)
If you use datamapper, there is an paperclip for datamapper!

regards!
Posted by Tony Tony (slythic)
on 09.08.2009 02:00
Marcos Vanetta wrote:
> If you use datamapper, there is an paperclip for datamapper!
> 
> regards!

Thanks for the reply! I'm using ActiveRecord and wouldn't want to change 
to Datamapper unless I HAD to. Thanks again, I'll look into it as soon 
as I get a chance.


-Tony
Posted by Brian Candler (candlerb)
on 10.08.2009 10:51
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"}
Posted by Dressup 9x (fashionsgames)
on 10.08.2009 11:10
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 by Tony Tony (slythic)
on 10.08.2009 14:21
Thank you Brian! I will give it a shot when I have some time and reply 
here. Truly appreciate it!


-Tony
Posted by Almaz OM (almazom)
on 24.07.2010 13:15
phanks:

post '/upload' do
    unless params[:file] &&
           (tmpfile = params[:file][:tempfile]) &&
           (name = params[:file][:filename])
      @error = "No file selected"
      return haml(:upload)
    end
      directory = "public/files"
      path = File.join(directory, name)
      File.open(path, "wb") { |f| f.write(tmpfile.read) }
end

so will be better for me :)
Posted by Brian Candler (candlerb)
on 30.07.2010 19:03
Almaz OM wrote:
> phanks:
> 
> post '/upload' do
>     unless params[:file] &&
>            (tmpfile = params[:file][:tempfile]) &&
>            (name = params[:file][:filename])
>       @error = "No file selected"
>       return haml(:upload)
>     end
>       directory = "public/files"
>       path = File.join(directory, name)
>       File.open(path, "wb") { |f| f.write(tmpfile.read) }
> end
> 
> so will be better for me :)

OK. Beware that f.write(tmpfile.read) will use as much RAM as the size 
of the attachment. Hence the suggestion to read it in and write it out 
in blocks of, say, 64K.