简体   繁体   中英

Rails 3 send_file - 0 bytes

I've got the following rails code

send_file '/test.pdf'

The file seems to be downloading with 0 bytes, has anyone got any ideas on how to fix this?

thanks

I believe that send_file depends on support from your web server to work. Are you running your app using the rails built in server? If so, I think you'll see the behaviour you've got here.

Basically, the idea behind send_file is that it sets an HTTP header 'X-Sendfile' pointing to the file you want to send. The web server sees this header and rather than returning the body of your response, sends the specified file instead.

The benefit of this approach is that your average web server is highly optimised for sending the content of static files very quickly, and will typically get the job done many times more quickly than rails itself can.

So, the solutions to your problem are to either: * Use a webserver that supports X-Sendfile (eg Apache), or * As rubyprince commented, use send_data instead, which will make ruby do the heavy lifting.

Just as an aside, you should be able to confirm that this is what's happening by looking at the response headers using either Firebug or Safari/Chrome's developer tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM