# Send\_file and remote files

**URL:** <https://www.ruby-forum.com/t/send-file-and-remote-files/86352>\
**Category:** Rails\
**Created:** [February 21, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352 "2007-02-21T18:04:01Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 21, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/1 "2007-02-21T18:04:01Z")

</div>

Hello,

My first post here. I’m quite new to Ruby.

I’m making a Flash application that loads external files  
from other servers onto the user’s hard disk.  
That’s why I need a way to connect to the remote locations  
over absolute http links; so I can monitor the progress inside  
my Flash app. So this code should act as a file serving proxy.  
Flash has some security restrictions, that will not allow the  
loading of external files from other domains.

What I’ve got is this (not working, throws an error).  
I used this file as an example:

“Cannot read file [http://www.twin-diamonds.com/pic/rubies.jpg](http://www.twin-diamonds.com/pic/rubies.jpg)”

class StreamDataController \< ApplicationController  
def index  
send\_file(params[:file],:filename =\> params[:name],  
:stream =\> ‘true’, :buffer\_size =\> ‘4096’)  
end  
end

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 21, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/2 "2007-02-21T22:37:53Z")

</div>

No one ?

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:18am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/3 "2007-02-22T00:18:15Z")

</div>

Mister T. wrote:

> What I’ve got is this (not working, throws an error).  
> I used this file as an example:
> 
> “Cannot read file [http://www.twin-diamonds.com/pic/rubies.jpg](http://www.twin-diamonds.com/pic/rubies.jpg)”
> 
> class StreamDataController \< ApplicationController  
> def index  
> send\_file(params[:file],:filename =\> params[:name],  
> :stream =\> ‘true’, :buffer\_size =\> ‘4096’)  
> end  
> end

send\_file is meant to work with local files. So ruby tries to open that  
string as a local filename. You might try send\_data with a string of  
data instead.

#environment.rb  
require ‘open-uri’

#controller  
def index  
data = open(params[:file])  
send\_data data, :filename =\> params[:name], …  
end

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:19am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/4 "2007-02-22T00:19:24Z")

</div>

Alex W. wrote:

> #environment.rb  
> require ‘open-uri’
> 
> #controller  
> def index  
> data = open(params[:file])  
> send\_data data, :filename =\> params[:name], …  
> end

Oops, I think that should be:

#controller  
def index  
data = open(params[:file]).read  
send\_data data, :filename =\> params[:name], …  
end

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:27am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/5 "2007-02-22T00:27:26Z")

</div>

Mister T. wrote:

> That way I cannot set the buffer.  
> That’s kind of anoying, because now it takes  
> a while to load the files ( \> 10mb ) into  
> the memory of the server machine.
> 
> I’ve got code working like that, but I really  
> want to make use of the buffer.

Then you may want to write the file to a temp directory and then send it  
with send\_file

data = open(params[:file])  
filename = “#{RAILS\_ROOT}/tmp/my\_temp\_file”

File.open(filename, ‘r+’) do |f|  
f.write data.read  
end

send\_file filename, …options…

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:20am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/6 "2007-02-22T00:20:06Z")

</div>

That way I cannot set the buffer.  
That’s kind of anoying, because now it takes  
a while to load the files ( \> 10mb ) into  
the memory of the server machine.

I’ve got code working like that, but I really  
want to make use of the buffer.

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:44am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/7 "2007-02-22T00:44:50Z")

</div>

I think that leaves me with the same issue, no ?

I will have to download the file first to my own server.  
And then serve them to my clients.  
The same thing happens with send\_data, but only to the  
server’s memory, in stead of writing the data to the hard disk.

The good thing is that I can cache the files this way.

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:52am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/8 "2007-02-22T00:52:07Z")

</div>

I tried your code but it gives:

No such file or directory - “[http://www](http://www)…”

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:56am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/9 "2007-02-22T00:56:52Z")

</div>

Mister T. wrote:

> I think that leaves me with the same issue, no ?
> 
> I will have to download the file first to my own server.  
> And then serve them to my clients.  
> The same thing happens with send\_data, but only to the  
> server’s memory, in stead of writing the data to the hard disk.
> 
> The good thing is that I can cache the files this way.

I don’t think you will be able to do this from a rails app. I just  
don’t see a way that you can download the file the file in chunks and  
sending those chunks back out before the file has downloaded completely.

Maybe some other background process on your server could handle a  
request like this, but I don’t think it will work from within Rails.

Although, it’s disturbing that I think that is the first time I have  
ever said that.

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 1:32am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/10 "2007-02-22T01:32:18Z")

</div>

Mister T. wrote:

> I’ve added those lines too now. sorry about that.
> 
> I have another error now.
> 
> No such file or directory - /var/www/vhosts/…/railsapp/tmp/download

Well that path isn’t vary well formed. That ‘…’ should probably be  
‘…/…’, so adjust the way you are constructing that path accordingly.

> When I send it to tmp/downloads ( I created that folder ),  
> then it says:
> 
> Is a directory - /var/www/vhosts/…/railsapp/tmp/downloads

You must supply the full filename of the file to write with File.open.  
If you want to use the same filename as the one being downloaded, try  
something like this, which will get everything to the right of the last  
‘/’ in the url.

url = ‘[http://foo.com/index.html](http://foo.com/index.html)’  
filename = url.split(‘/’).last  
full\_path = “#{RAILS\_ROOT}/tmp/#{filename}”

File.open(full\_path, ‘r+’) {|f| … }

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 12:58am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/11 "2007-02-22T00:58:12Z")

</div>

Mister T. wrote:

> I tried your code but it gives:
> 
> No such file or directory - “[http://www](http://www)…”

did you remember this from my previous post?

#environment.rb  
require ‘open-uri’

open-uri lets the “open” method work with urls.

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 1:07am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/12 "2007-02-22T01:07:14Z")

</div>

I’ve added those lines too now. sorry about that.

I have another error now.

No such file or directory - /var/www/vhosts/…/railsapp/tmp/download

When I send it to tmp/downloads ( I created that folder ),  
then it says:

Is a directory - /var/www/vhosts/…/railsapp/tmp/downloads

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 1:41am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/13 "2007-02-22T01:41:13Z")

</div>

The error still occurs.

No such file or directory -  
/var/www/vhosts/…obfuscatedPath…/railsapp/tmp/downloads/rubies.jpg

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 1:36am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/14 "2007-02-22T01:36:20Z")

</div>

The … is for obfuscating my paths.  
The path is correct.

I’ll try it now.

Tnx Alex ; )

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 1:44am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/15 "2007-02-22T01:44:39Z")

</div>

Mister T. wrote:

> The error still occurs.
> 
> No such file or directory -  
> /var/www/vhosts/…obfuscatedPath…/railsapp/tmp/downloads/rubies.jpg

Are you sure that this path resolves to an existing directory?

/var/www/vhosts/…obfuscatedPath…/railsapp/tmp/downloads/

The File.open method will not create directories, only files. So all  
directories must be in place.

If it still doesn’t work, I dont know what else to tell you besides to  
read up on file reading and writing in ruby.

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 2:40am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/16 "2007-02-22T02:40:59Z")

</div>

Alex, is this a typo ?

File.open(filename, ‘r+’) do |f|  
f.write data.read  
end

shouldn’t this be:

File.open(filename, ‘w’) do |f|  
f.write data.read  
end

When I try this it gives me “can’t convert String into Integer”

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 2:03am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/17 "2007-02-22T02:03:52Z")

</div>

I just started a brand new SSH session.  
I typed in the path and it fully resolves ( excluding rubies.jpg off  
course ).

I even chowned the tmp/downloads dir to 777

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [February 22, 2007, 3:10am UTC](https://www.ruby-forum.com/t/send-file-and-remote-files/86352/18 "2007-02-22T03:10:32Z")

</div>

I changed the “r+” attribute to “w”  
and also discovered that buffer\_size=\>‘4096’ had to be buffer\_size=\>4096  
without the quotes.

Thanks Alex !
