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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Download Problem 1

Status
Not open for further replies.

jimmythegeek

Programmer
May 26, 2000
770
US
I found some code to download files through asp, and it seems to work correctly. However, when I download any kind of text file (txt, asp, htm, etc), the file is blank when it gets to my hard drive. I checked the file on the server and it is fine, but it basically erases everything on the download. Below is the code I use, any thoughts? Thanks in advance.

==========================================================
some code ..........

on error resume next
response.Buffer = True
response.Clear
set stream = server.createObject("ADODB.Stream")
stream.Open
stream.Type = 1

set fso = Server.CreateObject("Scripting.FileSystemObject")
set f = fso.GetFile(filePath)
intFilelength = f.size
stream.LoadFromFile(filePath)

strExt = lcase(right(filePath, 4))
select case strExt
case ".txt", "csv", ".inf", ".asp", ".log", ".cls", ".dsw", ".inc", ".dsp", ".ini"
contentType = "text/plain"
case "htm"
contentType = "text/html"
case ".asf"
contentType = "video/x-ms-asf"
case ".avi"
contentType = "video/avi"
case ".doc"
contentType = "application/msword"
case ".zip"
contentType = "application/zip"
case ".xls"
contentType = "application/vnd.ms-excel"
case ".gif"
contentType = "image/gif"
case ".jpg", "jpeg"
contentType = "image/jpeg"
case ".wav"
contentType = "audio/wav"
case ".mp3"
contentType = "audio/mpeg3"
case ".mpg", "mpeg"
contentType = "video/mpeg"
case ".rtf"
contentType = "application/rtf"
case else
'Handle All Other NON-Text Files
ContentType = "application/octet-stream"
end select

with response
.AddHeader "Content-Disposition", "attachment; filename=" & f.name
.AddHeader "Content-Length", intFilelength
.CharSet = "UTF-8"
.ContentType = contentType
.BinaryWrite s.Read
.flush
end with

s.Close
set s = Nothing
====================================================

Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
perhaps it's because you're doing a binarywrite on text data?


second note you're not closing/clearing your F or FSO objects
 
and where does s come in?

.binary.write s.read


Chris.


Indifference will be the downfall of mankind, but who cares?
 
As chris pointed out, that s.read and subsequent s's should be stream. Is it possiblwe you went back and renamed the variable starting at the top and missed the bottom occurrences?

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
This code was what I was looking for, and yes Tarwn there is correct that the code doesn't work until you change the s to stream. Especially the

.BinaryWrite s.Read

line.

-k

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top