Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asptoday_discuss thread: Downloading Files Using the Stream Object


Message #1 by "Lawrence Yap" <assoc_con@y...> on Fri, 22 Mar 2002 00:59:06
Hello Everyone,

     I have quite a dilemma with my application.  What it does is to let 
users download files from the web coming from a database and I'm using the 
Stream Object to do that.  Apparently, when a user downloads a file, it 
opens up a dialog box for them to view the file or save it.  What happens 
is that, when the process of downloading or saving is cancelled when the 
user clicks on the "Cancel" button, it hangs the whole session or 
instances of that web page.  The only workaround right now is for users to 
close their browsers and relaunch it again.  If a user finishes the 
download, then it's fine.  It seems that when a "cancel" is done, the 
stream is left open and so in the background, it might still be running 
thus hanging the web pages.  I tried playing around with the EOS property 
to force a cancel and close the "stream" but to no avail.

     Has anyone had problems with this before and if you can share your 
solutions, that'll be greatly appreciated!

thank you,
Lawrence

ps I've attached the sample code as well.

<%
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName

Const adTypeBinary = 1

strFilePath = Request.QueryString("File")
strFileSize = Request.QueryString("Size")
strFileName = Request.QueryString("Name")

Response.Clear

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

strFileType = lcase(Right(strFileName, 4))
    
	' Feel Free to Add Your Own Content-Types Here
    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=000" & strFileName
	Response.AddHeader "Content-Length", strFileSize
	Response.Charset = "UTF-8"
	Response.ContentType = ContentType
	
	Response.BinaryWrite objStream.Read
		if Response.EOS = 1 THEN
			objStream.Cancel
			Response.Flush
			objStream.Close
			Set objStream = Nothing
			Response.End
		Else
			Response.Flush
		End If
	
		objStream.Close
		Set objStream = Nothing
	Response.End

%>

  Return to Index