...

View Full Version : Making TXT files downloadable



IFeelYourPain
05-09-2009, 05:17 AM
How do I go about making my txt files download instead of open in a new window/tab?

effpeetee
05-09-2009, 09:36 AM
Not sure what you mean. How about some examples or better still a link?

Frank

abduraooft
05-09-2009, 10:22 AM
How do I go about making my txt files download instead of open in a new window/tab?
You could add a statement in your .htaccess file, to forcibly prompt for download the file, like

AddType application/octet-stream .txt

IFeelYourPain
05-09-2009, 06:15 PM
You could add a statement in your .htaccess file, to forcibly prompt for download the file, like

AddType application/octet-stream .txt

That's exactly what I was looking for thanks man!

ALSO: If I include a text file with php will your code force the text file to download instead of including itself within a document.


Not sure what you mean. How about some examples or better still a link?

Frank

abduraooft knew what I was talking about without any examples. However to further explain his code it forces the text file to download instead of displaying the text file in a new tab/window.

abduraooft
05-09-2009, 06:20 PM
ALSO: If I include a text file with php will your code force the text file to download instead of including itself within a document.
How about having a try? :)

IFeelYourPain
05-09-2009, 07:45 PM
How about having a try? :)

Yea it works only one little issue.

Instead of line breaks it replaces them with little square boxes.

EX:
http://www.consolediscussions.com/forum/codes/USA/A/ULUS-10251.txt

abduraooft
05-10-2009, 12:37 PM
Yea it works only one little issue.

Instead of line breaks it replaces them with little square boxes.

EX:
http://www.consolediscussions.com/forum/codes/USA/A/ULUS-10251.txt
Sorry, I don't get your current issue.

IFeelYourPain
05-10-2009, 04:59 PM
Sorry, I don't get your current issue.

If you download the TEXT file, it won't put it in the format I uploaded it to the server.

Instead of:
This
Code
Is
Cool

It shows as:
This[]Code[]Is[]Cool

How can I prevent this?

abduraooft
05-10-2009, 05:45 PM
It has the line breaks when opened in notepad++. Not sure why notepad behaves like that.

IFeelYourPain
05-12-2009, 09:24 PM
It has the line breaks when opened in notepad++. Not sure why notepad behaves like that.

Is there a way to fix it, because I am sure many of my members will complain.

timgolding
05-16-2009, 02:27 PM
Personally I don't think its a good idea having your users download all those text files. Would annoy the hell out of me having lots of text files all over the desktop. If you can't manage to get the popups working then you could always go for getting the files to open in a new tab/window with target="_blank"



<a href="/Files/A/Annoying.txt" target="_blank">View</a>


Another thing to consider is what program is opening the text files. For instance notepad will be looking for an ascii r\n (carriage return and newline) while other programs might except just \n or \r. Different programs expect different end of line character combinations. However if it opens a text file in a browser you see the newlines as they were formatted.

http://cybadev.co.uk/example.txt


P.S look at the source to the above link that is an example of a target="_blank" anchor

abduraooft
05-16-2009, 02:32 PM
Would annoy the hell out of me having lots of text files all over the desktop. It's up to the user to decide the location to which the file to be saved.

timgolding
05-16-2009, 03:47 PM
It's up to the user to decide the location to which the file to be saved.
Yeah i know but considering what IFeelYourPain is trying to do which is not fully stated in this thread. Look at this thread for a clearer idea.

http://www.codingforums.com/showthread.php?t=165918

I don't see that making these popups download links is the way to go.

IFeelYourPain
05-16-2009, 07:44 PM
Yeah i know but considering what IFeelYourPain is trying to do which is not fully stated in this thread. Look at this thread for a clearer idea.

http://www.codingforums.com/showthread.php?t=165918

I don't see that making these popups download links is the way to go.

Your mixing up my features lol. The popups are to preview the text files and the download feature is actually to download. I guess it would be best to show you a live demo:
http://www.consolediscussions.com/forum/codes.php

The popups will actually be used to load the text file into an Ajax popup which will then format the text so that it previews.

timgolding
05-18-2009, 01:03 AM
Ah ok my apologies. Seems like you got it the downloads working? Where are the popups?

IFeelYourPain
05-19-2009, 06:27 AM
Ah ok my apologies. Seems like you got it the downloads working? Where are the popups?

Like I said download works fine, but when you save the TXT file and open it in notepad it makes the words combined:
ThisIsATest

Instead of:
This
Is
A
Test

The popups haven't been messed with yet, having some host issues.

timgolding
05-19-2009, 10:22 AM
Ah ok. Well as i said in a previous post thats because notepad uses \r\n for its line breaks. Thats an ascii code 13 and a 10 (Carriage Return/Line Feed). Whatever editor you used to create them is putting only one of them either \r or \n. So the only fix is either using notepad to create them or using a script to replace the codes.

Shannon Blonk
05-19-2009, 05:20 PM
Browsers will generally do the line-ending conversions for you -- changing them to \r\n on Windows, \r on Mac, \n on Linux -- if you let them.

Setting the MIME type to application/octet-stream tells the browser NOT to convert. Use the proper text/plain or text/whatever; and set the Content-Disposition header to attachment, optionally with a filename.

See rfc2183.

IFeelYourPain
05-20-2009, 05:44 AM
Browsers will generally do the line-ending conversions for you -- changing them to \r\n on Windows, \r on Mac, \n on Linux -- if you let them.

Setting the MIME type to application/octet-stream tells the browser NOT to convert. Use the proper text/plain or text/whatever; and set the Content-Disposition header to attachment, optionally with a filename.

See rfc2183.

You lost me. Mind explaining in more detail?

Shannon Blonk
05-20-2009, 07:15 AM
Different operating system have different ideas about what indicates the end of line in a text file. Linux (which it seems your server is running) uses ASCII character 10, also known as newline or linefeed or LF, and often seen in code as \n.

Windows likes the two character sequence ASCII 13, ASCII 10 -- aka carriage return/linefeed otherwise refered to as CR/LF or as \r\n. When notepad gets a Linux-style file, it doesn't see any line-breaks -- just weird lonely newlines that it shows as little boxes.

Browsers, meanwhile, are used to speaking with various OSes and will change line endings to match what you are using. But of course you don't want a browser messing with the bytes of a program or a picture, do you? Hence MIME-types or content-types, which browsers use to figure out what to do.

When you add 'AddType application/octet-stream .txt' to .htaccess and send a text file, the browser see octet-stream (byte stream) and thinks "All these bytes are important, I better save it unaltered." No handy line-end converstions.

Back to MIME and it's e-mail origins. Some things are meant for immediate use (like an inline image) and some things are meant to be saved. Content-Disposition!

The http headers you want to send are
Content-type: text/plain
Content-Disposition: attachment; filename="somefilename.txt"

If you have mod_headers this should work (not an Apache expert)
<Files "*.txt">
Header set Content-Disposition "attachment"
</Files>
though you might be better off using a handler that serves your text files and sets filename correctly.

http://en.wikipedia.org/wiki/MIME

IFeelYourPain
05-29-2009, 01:40 AM
Different operating system have different ideas about what indicates the end of line in a text file. Linux (which it seems your server is running) uses ASCII character 10, also known as newline or linefeed or LF, and often seen in code as \n.

Windows likes the two character sequence ASCII 13, ASCII 10 -- aka carriage return/linefeed otherwise refered to as CR/LF or as \r\n. When notepad gets a Linux-style file, it doesn't see any line-breaks -- just weird lonely newlines that it shows as little boxes.

Browsers, meanwhile, are used to speaking with various OSes and will change line endings to match what you are using. But of course you don't want a browser messing with the bytes of a program or a picture, do you? Hence MIME-types or content-types, which browsers use to figure out what to do.

When you add 'AddType application/octet-stream .txt' to .htaccess and send a text file, the browser see octet-stream (byte stream) and thinks "All these bytes are important, I better save it unaltered." No handy line-end converstions.

Back to MIME and it's e-mail origins. Some things are meant for immediate use (like an inline image) and some things are meant to be saved. Content-Disposition!

The http headers you want to send are
Content-type: text/plain
Content-Disposition: attachment; filename="somefilename.txt"

If you have mod_headers this should work (not an Apache expert)
<Files "*.txt">
Header set Content-Disposition "attachment"
</Files>
though you might be better off using a handler that serves your text files and sets filename correctly.

http://en.wikipedia.org/wiki/MIME

Considering I need it do do this with all .txt files is it possible to remove the filename variable altogether?

Shannon Blonk
05-29-2009, 10:21 PM
Sure. The filename part is mostly used when you have a script that can serve up multiple files, or generate them on the fly, or somesuch.

For example http://kittens.cute/stories.pl?name=princess&type=pdf would be auto-saved as "stories.pl" if the filename part wasn't used (when what you probably want is princess.pdf) -- potentially overwriting the previous story that had been downloaded. With the filename specification, it'd be easy to keep your kitty tales straight.

But as long at you are serving simple txt files with unique names, the filename spec is unneed.

IFeelYourPain
06-06-2009, 02:33 AM
Sure. The filename part is mostly used when you have a script that can serve up multiple files, or generate them on the fly, or somesuch.

For example http://kittens.cute/stories.pl?name=princess&type=pdf would be auto-saved as "stories.pl" if the filename part wasn't used (when what you probably want is princess.pdf) -- potentially overwriting the previous story that had been downloaded. With the filename specification, it'd be easy to keep your kitty tales straight.

But as long at you are serving simple txt files with unique names, the filename spec is unneed.

I tried both of these and they both serve up a blank page when I click the download link for the txt file.


Content-type: text/plain
Content-Disposition: attachment;


AddType application/octet-stream .txt
Content-type: text/plain
Content-Disposition: attachment;

IFeelYourPain
06-11-2009, 05:27 AM
Still need help on this issue.

IFeelYourPain
06-24-2009, 07:55 PM
Still getting a blank page. :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum