Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download to file without creating a file

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hey guys and gals
I would like force a download of a file that is created in memory.

I have a string that I was saving to a text file. You then download the textfile.

I would like to somehow miss the save step and just go straight to download, is this possible with binarywrite and streams or somthing?

Cheers

}...the bane of my life!
 
If you have the data in a binaryformat already you could write it back to the browser in binary format and change the header on the page to force a download.

Code:
Function ReadBinaryFile(FileName)
	Const adTypeBinary = 1
	Dim BinaryStream
	Set BinaryStream = Server.CreateObject("ADODB.Stream")
	BinaryStream.Type = adTypeBinary
	BinaryStream.Open
	BinaryStream.LoadFromFile FileName
	ReadBinaryFile = BinaryStream.Read
End Function

myFile = Request.QueryString("fileName")
myExtension = mid(myFile,instr(myFile,".") + 1)
myFileType = "Text"
pathName = Request.QueryString("Path")
myPath = server.MapPath(pathName & myFile)

if myExtension = "EPS" or myExtension="GIF" or myExtension="JPG" then
	myFileType = "image"
end if
if myExtension = "PDF" then
	myFileType = "application"
end if

dim myStream
myStream = ReadBinaryFile(myPath)
Response.Buffer = True
Response.AddHeader "content-disposition", "attachment; filename=" & myFile
Response.AddHeader "content-length", Len(mystream)
Response.ContentType = myFileType & "/" & myExtension
Response.BinaryWrite myStream
Response.Flush
{/code]

I use this code to force a download of PDF's, EPS, GIF and JPG files from one of our applications to the client machine.

Notice the Response.AddHeader statements and the Response.ContentType.

What you are doing is sending the data to the browser, then telling the browser not to handle it as content for the browser.

The example above opens takes a file, reads it as binary data then sends it to the browser so that the browser opens a save file dialogue box.

I'm only handling PDF, EPS JPG and GIF in this example.  If you want to use something else, you need to find out what to set for the different content types.

To build may have to be the slow laborious task of years.  To destroy can simply be the thoughtless act of a single day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top