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

T

Tony Tony

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
 
M

Marcos Vanetta

[Note: parts of this message were removed to make it a legal post.]

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

regards!
 
T

Tony Tony

Marcos said:
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
 
B

Brian Candler

Tony said:
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"}
 
B

Bin Bin

Brian said:
Tony said:
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
 
T

Tony Tony

Thank you Brian! I will give it a shot when I have some time and reply
here. Truly appreciate it!


-Tony
 
A

Almaz Om

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 :)
 
B

Brian Candler

Almaz said:
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.
 
B

Bohdan S.

Brian Candler wrote in post #929342:
Almaz said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top