How to make a file-download? Code help needed

Discussion in 'Ruby/Rails' started by beakid, Dec 13, 2007.

  1. beakid

    beakid New Member

    Hi!

    Have searched this forum for an answer but haven't found anything helpful.

    What should I write to make a jpg for example download when pushing a link?

    Right now my code is this:
    This is the code I have found on threads here in the forum, but it doesn't work for me. I have enabled sendfile() in the litespeed config (don't know if that helps thought).

    How should I write? All that is happening right now is that the jpg gets rendered.
  2. mistwang

    mistwang LiteSpeed Staff

    try adding a "Content-disposition" header.
  3. mistwang

    mistwang LiteSpeed Staff

    You may need to override the MIME type for jpg file to something like application/octect-stream under your downloads folder.
  4. beakid

    beakid New Member

    added this now

    Still just showing the image and getting this in the log:
    Getting ActionController::MissingTemplate (Missing template /home/user/railsapp/web/app/views/search/download_path.rhtml):
  5. mistwang

    mistwang LiteSpeed Staff

    Adding "Content-Type" header would not work, you need to either override the MIME with "AddType ..." in .htaccess under the directory holding those files, or create a static "Context" with MIME override in LSWS console for the directory holding the files.
  6. beakid

    beakid New Member

    Thanks for the quick respons. Have now created a .htaccess file in the downloads-folder and checked Fileinfo in the HT Access-part of the LiteSpeed config.

    My .htaccess file looks like this:

    Tried accessing http://192.168.1.203:8088/search/download_path/1663912/mmm.jpg but still with no download-popup-luck :/ What am I missing?

    EDIT: YES! Got it working! The problem was that I had checked Fileinfo in general settings and not in my rails settings :) Thanks.
  7. beakid

    beakid New Member

    ... on a sidenote, how can i make this work for ALL filetypes, without having to type them all in? wildcard or nothing at all won't work.
  8. mistwang

    mistwang LiteSpeed Staff

    Use "ForceType" in .htaccess for that folder.
  9. beakid

    beakid New Member

    Ok, it's me again.

    My .htaccess only have this now:
    This does not work. If I use this one (just for testing)
    the jpg gets rendered as text/html BUT only every other reload. Must often it's rendered like a normal jpg. Is this a litespeed-bug? Have tried many browsers, so it's not browser-based. The same is true if I only have this
    The download-windows only occurs every other reload :(

    EDIT:
    Also tried this one which I found on a forum from a guy stating it's the simpliest way to force download in i folder:
    Does not work at all :(
    Last edited: Dec 18, 2007
  10. mistwang

    mistwang LiteSpeed Staff

    Some browser caches the reply, so you have to clear your browser cache after change any configuration.

    Or, use a command tool to test it, like "lynx -mime_header ...",
    wget, curl, etc, all have command line option to show the response header, you can verify the content-type this way.
  11. beakid

    beakid New Member

    Ok, tried lynx now. Still it behaves weird. My .htaccess-file looks like this now
    When I try accessing with Lynx directly after a liteserver-reboot I get this:

    22 seconds later, without doing anything different and without changing anything, I get this :

    This feels like a bug in Litespeed.
    Last edited: Dec 19, 2007
  12. mistwang

    mistwang LiteSpeed Staff

    for the 500 status code, please try adding "Status: 200 OK" header, see if it help.
    The changing MIME type does look like a bug.

    Is it possible that you send us a simple test application to reproduce the problem?
  13. mistwang

    mistwang LiteSpeed Staff

    I tried a simple test application, fixed a configuration problem with "Allow Override" in rails context. it will be in 3.3.4 release. However, if you create a static context for the download folder and set "Allow Override" there, it should work fine with 3.3.3 release.

    500 status code is caused by Rails internal error, like the missing index.rhtml, all errors in rails log file should be fixed.
  14. beakid

    beakid New Member

    Thanks mistwang. Got it working finally with the 3.3.3 release (had 3.1.2 before).

    For future readers, this solution works for me:

    1. I put a .htaccess in my downloads-folder, containing:
    2. Activated full HT Access in the General Settings in LiteSpeed Web Admin > Configuration > Server > General and LiteSpeed Web Admin > Configuration > Virtual Host Template > EasyRailsWithSuEXEC > General
    3. Added the Rewrite rule to secure my folder from direct access:
    Last edited: Jan 8, 2008
  15. James

    James New Member

    Thanks for the quick respons. Have now created a .htaccess file in the downloads-folder and checked Fileinfo in the HT Access-part of the LiteSpeed config.

Share This Page