Link to home
Start Free TrialLog in
Avatar of ephemeralz
ephemeralz

asked on

how to stream file

How can I properly stream file in ASP? The user would be clicking through a link which call a asp page with a url encode of the filename. The link looks like this:
http://../files.asp?f=D%3A%5CSandBoxes%5CEPZ%Support+Tech%5Croot%5Cdownloads%5CNew+Winamp+media+file%2Ewav&e=ephz@mail.com

And here's the codes for files.asp:


<%

Dim email, filename, dFiles,  msg
Dim strSQL
filename=request("f")
email = Request("e")

strSQL = "SELECT * FROM EmailDownloads WHERE email = '" & email "' AND filename = '" & filename & "'"
rs.Open strSQL, objConn, adOpenKeyset, adLockPessimistic

If Not rs.EOF Then

dim objFSO, objTS
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(fileName)

sFileType= Right(fileName,4)

Select Case sFileType
            Case ".asf"
                sContentType = "video/x-ms-asf"
            Case ".avi"
                sContentType = "video/avi"
            Case ".doc"
                sContentType = "application/msword"
            Case ".zip"
                sContentType = "application/zip"
         Case ".css"
                sContentType = "text/css"
      Case ".pdf"
                sContentType = "application/pdf"
            Case ".xls"
                sContentType = "application/vnd.ms-excel"
            Case ".gif"
                sContentType = "image/gif"
            Case ".jpg", "jpeg"
                sContentType = "image/jpeg"
            Case ".wav"
                sContentType = "audio/wav"
            Case ".mp3"
                sContentType = "audio/mpeg3"
            Case ".mpg", "mpeg"
                sContentType = "video/mpeg"
            Case ".rtf"
                sContentType = "application/rtf"
         Case ".htm", "html"
                sContentType = "text/html"
         Case ".asp", ".asa"
                sContentType = "text/asp"
            Case Else
                sContentType = "application/x-msdownload"
End Select

NameFile=Right(FileName,Len(FileName)-InstrRev(FileName,"\"))

Response.ContentType = sContentType
response.AddHeader "Content-Disposition", "attachment;filename=" & NameFile

    Response.Buffer = True
    Do While Not objTS.AtEndOfStream
      strChunk = objTS.Read(32)
      strTmp = ""
      For i = 1 to Len(strChunk)
           strTmp = strTmp & ChrB(Asc(Mid(strChunk, i, 1)))
      Next
      Response.BinaryWrite strTmp
      Response.Flush
    Loop
    objTS.Close
    Set objTS = Nothing
    Set objFSO = Nothing

Else
Response.Redirect "login/login.asp"
Response.End
End If

%>

Problem:
How to decode the url encode string?
The file is stored locally on the server directory.  As you can see I also used Server.MapPath for the file location. Is this the correct way to stream file content in ASP?

Thanks!
Avatar of ephemeralz
ephemeralz

ASKER

The size of the file to be streamed ranges from 2mb - 50mb.
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

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
I'm having trouble streaming the file in asp. For the filename, it's showing the URLEncode of the Server.Mappath and the filetype is showing blank. I have tried this and it works in one place and for some reason it's not working now. Any ideas?
Thanks!

<%

Dim email, filename

filename=request("f")
email = Request("e")

sFileType= Right(filename,4)

Select Case sFileType
            Case ".asf"
                sContentType = "video/x-ms-asf"
            Case ".avi"
                sContentType = "video/avi"
            Case ".doc"
                sContentType = "application/msword"
            Case ".zip"
                sContentType = "application/zip"
         Case ".css"
                sContentType = "text/css"
      Case ".pdf"
                sContentType = "application/pdf"
            Case ".xls"
                sContentType = "application/vnd.ms-excel"
            Case ".gif"
                sContentType = "image/gif"
            Case ".jpg", "jpeg"
                sContentType = "image/jpeg"
            Case ".wav"
                sContentType = "audio/wav"
            Case ".mp3"
                sContentType = "audio/mpeg3"
            Case ".mpg", "mpeg"
                sContentType = "video/mpeg"
            Case ".rtf"
                sContentType = "application/rtf"
         Case ".htm", "html"
                sContentType = "text/html"
         Case ".asp", ".asa"
                sContentType = "text/asp"
            Case Else
                sContentType = "application/x-msdownload"
End Select

NameFile=Right(filename,Len(filename)-InstrRev(filename,"\"))


Response.ContentType = sContentType

Response.AddHeader "Content-Disposition", "attachment;filename=" & NameFile

Const adTypeBinary = 1
Dim strFilePath
strFilePath = filename

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

Response.BinaryWrite objStream.Read

objStream.Close
Set objStream = Nothing
It seems like I'm getting a Type Mismatch error at this line:
Response.BinaryWrite objStream.Read
Okay, well, we need to debug it a little.  Instead of sending the file content, temporarily comment out those lines, and add lines that display the values of these variables:
     filename
     strFilePath
     NameFile
     sContentType

Paste those values in this thread, and I'll see what I can figure out from that.

Dex*
Thanks for the help! The problem is that I'm testing files with 0 bytes.
Hehe... Yeah, that would do it.  :)  Glad you got it working...

Dex*