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
You are reading whole file into memory!
Use
send_filewhich 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/