How to make a file-download? Code help needed

#1
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:
def download_path( _id = params[:id])
result = Philestructure.find( _id )
#code checking for permission here. if true, go on
headers['X-LiteSpeed-Location'] = "/downloads/#{result.phile.file_name}"
end
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.
 
#4
added this now

headers['Content-Disposition'] = "attachment; filename=/downloads/#{result.phile.file_name};"
headers['Content-Type'] = "application/octect-stream"
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):
 

mistwang

LiteSpeed Staff
#5
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
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:

AddType application/octect-stream jpg
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
... 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.
 
#9
Ok, it's me again.

My .htaccess only have this now:
ForceType application/octect-stream
This does not work. If I use this one (just for testing)
ForceType text/html
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
AddType application/octect-stream jpg
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:
<Files *.*>
ForceType applicaton/octet-stream
</Files>
Does not work at all :(
 
Last edited:

mistwang

LiteSpeed Staff
#10
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
Ok, tried lynx now. Still it behaves weird. My .htaccess-file looks like this now
ForceType text/html
When I try accessing with Lynx directly after a liteserver-reboot I get this:

beakid@safecube:~$ lynx -mime_header -cache=0 http://192.168.1.203:8088/search/download_path/1663912/mmm.jpg
HTTP/1.0 500 Internal Server Error
Date: Wed, 19 Dec 2007 08:59:45 GMT
Server: LiteSpeed
Accept-Ranges: bytes
Connection: close
ETag: "46650-475e70ab-2fc017"
Last-Modified: Tue, 11 Dec 2007 11:12:43 GMT
Content-Type: text/html
Content-Length: 230113
Content-Encoding: gzip
Vary: Accept-Encoding
22 seconds later, without doing anything different and without changing anything, I get this :

beakid@safecube:$ lynx -mime_header -cache=0 http://192.168.1.203:8088/search/download_path/1663912/mmm.jpg
HTTP/1.0 500 Internal Server Error
Date: Wed, 19 Dec 2007 09:00:07 GMT
Server: LiteSpeed
Accept-Ranges: bytes
Connection: close
ETag: "46650-475e70ab-2fc017"
Last-Modified: Tue, 11 Dec 2007 11:12:43 GMT
Content-Type: image/jpeg
Content-Length: 288336
This feels like a bug in Litespeed.
 
Last edited:

mistwang

LiteSpeed Staff
#12
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?
 

mistwang

LiteSpeed Staff
#13
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
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:
ForceType application/octet-stream
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:
RewriteCond %{ORG_REQ_URI} ^/downloads/
RewriteRule ^/downloads/ - [R=403,F]
 
Last edited:
#15
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.
 
Top