Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Force Download fails when I select "Open" but works fine when I select "Save"

Reply
Thread Tools

Force Download fails when I select "Open" but works fine when I select "Save"

 
 
Michael
Guest
Posts: n/a
 
      01-25-2005
I have a problem forcing files to download. If I select Save the document is
saved with no problems. If I select "Open" the document is empty or I get a
"File not found" error from the application opening the file...

Code:

function UTIL_ForceDownloadFile(strFileName, strFileSaveName)
Response.Buffer = True
Dim strFileSize
Const adTypeBinary = 1
Response.Clear

'#Code Find File Size
dim fso, fileObject
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fileObject = fso.GetFile(Server.MapPath(strFileName))
strFileSize = fileObject.Size
Set fileObject = Nothing
Set fso = Nothing

dim objStream, strFileType, ContentType
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile Server.MapPath(strFileName)

strFileType = lcase(Right(strFileName, 4))

Select Case strFileType
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 ".pdf"
ContentType = "application/pdf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select

Response.AddHeader "Content-Disposition", "attachment; filename=" &
strFileSaveName
Response.AddHeader "Content-Length", strFileSize

Response.ContentType = ContentType
Response.Charset = "UTF-8"
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
response.end
end function


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
Site with JavaScript fails to render page in IE 6 or 7 but fine with FF JJA Javascript 3 04-10-2007 09:20 PM



Advertisments