# Send\_file not working as expected

**URL:** <https://www.ruby-forum.com/t/send-file-not-working-as-expected/71715>\
**Category:** Rails\
**Created:** [October 10, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/send-file-not-working-as-expected/71715 "2006-10-10T23:57:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 10, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/send-file-not-working-as-expected/71715/1 "2006-10-10T23:57:14Z")

</div>

When I try to download a very large file (700MB) using send\_file I get  
a NoMemoryError (failed to allocate memory) error. It does look like I  
am really running out of memory, because I am watching the memory grow  
when this piece of code excutes. What typically happens on the client  
side is that it sends only 256MB of the file to be downloaded. I am  
stumped and not sure if this is a Windows, Mongrel, Rails, or ruby  
issue. I thought send\_file was supposed to stream a file in small  
packets so file size wasn’t an issue, but this doesn’t seem to be the  
case.

My set up is a Rails App. running on Windows Server 2003, Apache 2.2.3  
w/ two Mongrel 0.3.13.3 services running.

Here is the controller code that is erring:

@document = Document.find(params[:id])  
send\_file(FILE\_UPLOAD\_ROOT + @document.url.strip)

Additionally, I have tried this:

@document = Document.find(params[:id])  
fn = FILE\_UPLOAD\_ROOT + @document.url.strip  
file\_name = File.basename(fn.gsub(’\’, ‘/’)).gsub(/[^\w.-]/,’\_’)  
send\_file(FILE\_UPLOAD\_ROOT + @document.url.strip, :streaming =\> true,  
:type =\> ‘application/octet-stream’, :disposition =\> ‘attachment’,  
:filename =\> file\_name)

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 11, 2006, 12:27am UTC](https://www.ruby-forum.com/t/send-file-not-working-as-expected/71715/2 "2006-10-11T00:27:17Z")

</div>

On 10/10/06, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @document = Document.find(params[:id])  
> fn = FILE\_UPLOAD\_ROOT + @document.url.strip  
> file\_name = File.basename(fn.gsub(‘\’, ‘/’)).gsub(/[^\w.-]/,‘\_’)  
> send\_file(FILE\_UPLOAD\_ROOT + @document.url.strip, :streaming =\> true,  
> :type =\> ‘application/octet-stream’, :disposition =\> ‘attachment’,  
> :filename =\> file\_name)
> 
> Any help would be appreciated!

Mongrel buffers the Rails response to a StringIO then sends to the  
client  
when the request is finished. I recommend using the X-Sendfile:  
/path/to/file header with Apache2 mod\_xsendfile instead of streaming  
from  
Rails.

jeremy
