Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do I upload an image with Sinatra (like Paperclip)?

Reply
Thread Tools

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

 
 
Tony Tony
Guest
Posts: n/a
 
      08-08-2009
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 via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Marcos Vanetta
Guest
Posts: n/a
 
      08-08-2009
[Note: parts of this message were removed to make it a legal post.]

If you use datamapper, there is an paperclip for datamapper!

regards!

 
Reply With Quote
 
 
 
 
Tony Tony
Guest
Posts: n/a
 
      08-09-2009
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 via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      08-10-2009
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 via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bin Bin
Guest
Posts: n/a
 
      08-10-2009
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/.

 
Reply With Quote
 
Tony Tony
Guest
Posts: n/a
 
      08-10-2009
Thank you Brian! I will give it a shot when I have some time and reply
here. Truly appreciate it!


-Tony
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Almaz Om
Guest
Posts: n/a
 
      07-24-2010
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 via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-30-2010
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bohdan S.
Guest
Posts: n/a
 
      04-05-2011
Brian Candler wrote in post #929342:
> 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.


"f.write(tmpfile.read)"?? Why so complex?...

post '/upload' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
File.copy(tempfile.path, "./files/#{filename}")
redirect '/'
end

http://tumblr.com/xciegm157

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Sinatra and Rails. Was: [ANN] Sinatra 1.1 released! Ed Howland Ruby 1 10-26-2010 06:58 AM
Sinatra : examples and FRAME. B. Randy Ruby 2 04-22-2009 08:12 AM
[ANN] Sinatra 0.9.1 released! Ryan Tomayko Ruby 1 03-03-2009 01:17 AM
[ANN] Sinatra 0.9.0 released! Ryan Tomayko Ruby 0 01-18-2009 10:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57