Link to home
Start Free TrialLog in
Avatar of higijj
higijj

asked on

Sending txt file to browser

Hi!

I've made an export function for my web application and I would like that when the user click Get Extracted Data link, he is showed the Open/Save dialog box instead of seeing this into his broswer.

I tried using something like:

Sending those two headers
Content-Disposition: filename=Extract.csv
Content-type: text/plain

and printing the data I want them to download..

Any pointers as how to accomplish this?

Thanks.
Avatar of higijj
higijj

ASKER

Ok..

it show the save dialog but with a bizzare filename which is the complete url ..

if I click save or open, it gives me this error msg:

Internet Explorer cannot download ... ** FILENAME **
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

Any Idea?
Avatar of Gary
<%
Response.Buffer = True
strFileName="extract.csv"
strFilePath=server.mappath(strFilename)
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(strfilepath)
strFileSize = f.size
set f=nothing: set fso=nothing
Const adTypeBinary = 1
Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "application/txt" ' change to the correct content type for your file
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = strFileType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
%>
Avatar of higijj

ASKER

the problem with this solution is that there is no actual file on the HD.

the data is generated on the fly.

so if the data is generated on the file you can do this....


<%
output = "This is the data that will be in the downloaded Text File!"
Response.Buffer = True
Response.Clear
strFileName = "output.txt"
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = "application/txt"
Response.BinaryWrite output
Response.Flush
%>
ASKER CERTIFIED SOLUTION
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of higijj

ASKER

I should binary write the output even for a text file?
No, I just found this page, and looking for this answer myself, I was trying to output a csv using that binary write, I was having all sorts of problems in trying to open in excel. Even though it looks the same, plain text such as CSV, TXT and HTML should all be written using a standard response.write
Avatar of higijj

ASKER

Hey neur0maniak,

how did you do it then ?
Use the same as BrianGEFF719's sample code, but replace "Response.BinaryWrite" with "Response.Write"
Avatar of higijj

ASKER

Great!

last quick question..

I put my web page onto my desktop .. docked on my desktop..

when I do the download thing, once completed it display a white page..
I then decided to open a small popup with a onload="window.close();" in the body so it closes itself upon completion which worked well until I realize that the html code was also in the download file.

How can I prevent this from happening?

Like once I outputed the output, how can I say stop, this is now a webpage.. ?!
easy, use a response.end in the right place.
Avatar of higijj

ASKER

Thx dude.