Forum Moderators: phranque

Message Too Old, No Replies

MIME in Apache

add extension

         

imad77

3:00 am on Apr 8, 2009 (gmt 0)

10+ Year Member



Hi,

I run a script from a Web page to download a file, this Perl/CGI script and these files to download are located in a Linux server. When I click on the link of the file to download from a Windows XP station via this Web page (IE), a dialog box will be displayed to ask me to "Open" or "Save" the file (.txt, .doc, .xls) but for some extensions like ".dat" or ".asc", the dialog box ask me to save it only and I have not an "Open" button.

How can I add these extensions (.dat, .xml and others) to Apache configuration to be able to open it with Textpad or Notepad?
I added these lines in this file: /etc/mime.types

text/plain asc txt dat

I added these lines in this file also: /etc/httpd/conf/httpd.conf

AddType text/plain .asc .txt .dat

But it does not work fine. I want to configure these MIME types in Apache to provide to a lot of users in the net to be able to open these files.

Can someone help me to fix this issue in the httpd.conf?

Thanks

Imad77

jdMorgan

12:46 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If these files are "piped" through your cgi script -- That is, if your script opens the requested file, reads it into a buffer, and then sends it to the client, then the script itself should add the "Content-Type: text/plain" HTTP Header before outputting the file contents.

For example, in PERL:


print("Content-Type: text/plain\n\n");

Note that only the last HTTP header sent by your script should have two line-enders. If you wish to send additional HTTP headers, all but the last one should end with a single "\n".

Jim

imad77

5:30 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Hi,

I tried it but it opens all of the files (.doc, .xls, .csv, .ppt, ....) in Internet Explorer instead Notepad.

I want to be able to open the files with specified extension like .dat, .asc and I know that we can perform it from Microsoft IIS but I want to be able to add these extensions as MIME types in APache configuration file.

thanks

jdMorgan

5:38 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Consider adding the "Content-Disposition: attachment; filename=<your_filename_goes_here>" HTTP header to the output, then.

Be aware that browsers and operating systems are likely to throw warnings when you attempt to do this, especially on Windows Vista.

Jim

imad77

5:57 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Hi,

I tried these commands and I get the same dialog box for .dat, .asc files and I don't get the Open button :

print $cgi->header(-type => 'text/plain\n\n',
-attachment => "$file",
'Content-length' => -s "./$file"
);

jdMorgan

6:16 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to properly specify the Content-Type, Content-Disposition, and Content-Length headers. Two of these appear to be malformed in your code. Also, you apparently disregarded my warning about line-enders posted above, so these headers will not work. Each header must end with "\n" except for the last one, which must end with "\n\n". This is an HTTP protocol requirement.

You can use the Live HTTP Headers add-on for Firefox/Mozilla browsers to see what headers your script is outputting. The results should show:


Content-Type: text/plain
Content-Disposition: attachment; filename=<your_filename_goes_here>
Content-Length: <number of bytes>

Also, all of this depends on your browser knowing what program(s) can be used to open the file. If the browser has not been configured with this information, then only the "Save" option will be offered. Keep in mind that you will have to explain this to your users as well.

Jim

imad77

6:41 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Can you explain me ?
Why do we add these line "text/plain asc txt dat" in this file: /etc/mime.types ?

Why do we add these lines "AddType text/plain .asc .txt .dat" in this file: /etc/httpd/conf/httpd.conf ?

Thanks a lot.

jdMorgan

9:45 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So that files served by Apache will be sent with the correct MIME-type.

However, this does not affect files sent by a script, which is why your script should handle these files as needed.

Jim