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

**URL:** <https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441>\
**Category:** Ruby\
**Created:** [August 9, 2009, 1:02am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441 "2009-08-09T01:02:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 9, 2009, 1:02am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/1 "2009-08-09T01:02:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 9, 2009, 1:42am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/2 "2009-08-09T01:42:44Z")

</div>

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

regards!

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 9, 2009, 2:00am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/3 "2009-08-09T02:00:18Z")

</div>

Marcos V. 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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 10, 2009, 10:51am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/4 "2009-08-10T10:51:41Z")

</div>

Tony T. 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”}

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 10, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/5 "2009-08-10T14:21:34Z")

</div>

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

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 10, 2009, 11:10am UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/6 "2009-08-10T11:10:46Z")

</div>

Brian C. wrote:

> Tony T. 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](http://dressup9x.com) is a best Fashion Games, Girl Games, Dressup Game

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [July 24, 2010, 1:15pm UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/7 "2010-07-24T13:15:14Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [April 5, 2011, 3:45pm UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/8 "2011-04-05T15:45:15Z")

</div>

Brian C. 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

> **[Post by @alfuken](https://www.tumblr.com/alfuken/874428235/upload-and-download-files-in-sinatra)**
>
> 💬 0  🔁 8  ❤️ 6 · Upload and download files in Sinatra · List
> 
> get '/' do list = Dir.glob("./files/\*.\*").map{|f| f.split('/').last} # render list here end
> 
> Upload
> 
> post '/' do tempfile = param…

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [July 30, 2010, 7:03pm UTC](https://www.ruby-forum.com/t/how-do-i-upload-an-image-with-sinatra-like-paperclip/173441/9 "2010-07-30T19:03:19Z")

</div>

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.
