views:

59

answers:

1

Hii all, I am trying to downaload large file in rails using send_data function ,but getting error :failes to allocate memory and when trying to download in chunks ,getting only chunk size file only ,below is my code ..

File.open(@containerformat.location,"rb"){ |f| @data = f.read(8888)} ext = File.extname(@containerformat.streamName)

        if ext == '' 
            extension = File.extname(@containerformat.location)
            send_data(@data,:filename => @containerformat.name+extension,
            :disposition => 'attachment')
        else   
            send_data(@data,:filename => @containerformat.streamName,
            :disposition => 'attachment')
        end

i think am not able to make loop work

A: 

You are reading whole file into memory! Use send_file which uses memory friendly buffered stream.

I would also suggest to use :x_sendfile here, then file may be served directly by front server (Apache, nginx, lighttpd) if proper module is available and configured. This gives very efficient downloads and prevents blocking rails instance by slow clients.

Read about "X-Sendfile" header. http://tn123.ath.cx/mod_xsendfile/

gertas
thanks but am onle using mongel ,no front server
AMIT
and how do i send data in chunks using send_data function ??
AMIT
while using send_file i got an error
AMIT
string contains null byte
AMIT
send_data and send_file send bytes to stream instantly so you cant invoke them twice in action, please attach you code with send_file - it already does what you try to achieve with "chunked send_data"
gertas
sorry i didn't get you
AMIT
means with above code i can'nt achieve chunked data download ,then how do i download large files and to use send_data or send_file is must foe me
AMIT
and could you please tell how do use send_file to downlaod files with above code i just change send_file instead of send_data in above code
AMIT
send_file @containerformat.location, :filename => "filename-here.ext" #location should be absolute
gertas
yaa did it and but when i try to download 284 mb of file my server
AMIT
creahed , now trying with google chorme not with IE
AMIT
for small file its(send_file) working fine but for large what should i do gertas
AMIT
It seems its mongrel issue here, try patch with http://code.google.com/p/ruby-mongrel-x-sendfile/ or try another ruby server or use front server with X-Sendfile . Generally X-Sendfile is recommended for production. For development and test environment plain send_file should be sufficient.
gertas
but i am new to rails how do i configure apache as front server means how to i use X-Sendfile and all that ,am running mongel thats it and runing it in development mode
AMIT
I suggest you to setup Apache2 with http://tn123.ath.cx/mod_xsendfile/ and Passenger http://www.modrails.com/ as mongrel replacement for production - much easier. Google for tutorials and howtos for your production OS.
gertas