Click to See Complete Forum and Search --> : Forced Download Error with .mp3 Files


Robertomac
08-16-2007, 03:13 AM
Hi

I am having a problem with a code snippet i have which forces a download. The code works fine with all the other files i have tried (.pdf, .jpg, .gif), but when i try and use it with a .mp3 file it causes problems. In firefox it downloads an empty file to the desktop, but in explorer it causes a browser error.

The code is as follows:

Const adTypeBinary = 1

strFilePath = "D:\sites\interskisnowsportschool.co.uk\www.interskisnowsportschool.co.uk\web\Content\Downloads\"
strFileName = Request.Form("download")
strFilePath = strFilePath & strFileName

Response.Clear

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

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 ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select

Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.Charset = "UTF-8"
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing

I have tried removing the two case lines which refer to the .mp3 file extension to force it into the 'Case Else', but this doesn't work either.

If anyone has any suggestions or alternatives i would be very grateful.

Thanks in anticipation.

Rob