.NEt download function wont work

M

Mark

Hi - I'm using ASP.Net 1.1 with VS.Net 2003.

I have a file upload/download web app - it uploads great, but my
download function doesn't work for SWF files. If I type the complete
path to them into the browser, and run from there, it works perfect -
but I don't want to let people see the URL - so instead, I have the
download call this function:

Dim FileToDownload As String = "test.swf"
Dim iStream As System.IO.Stream
' Buffer to read 10K bytes in chunk:
Dim buffer(10000) As Byte
' Length of the file:
Dim length As Integer
' Total bytes to read:
Dim dataToRead As Long
' Identify the file to download including its path.
Dim filepath As String = "c:\" & FileToDownload
' Identify the file name.
Dim filename As String =
System.IO.Path.GetFileName(filepath)
Try
' Open the file.
iStream = New System.IO.FileStream(filepath,
System.IO.FileMode.Open, _

IO.FileAccess.Read, IO.FileShare.Read)
' Total bytes to read:
dataToRead = iStream.Length
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition",
"attachment; filename=" & filename)
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output
stream.
Response.OutputStream.Write(buffer, 0,
length)
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -1
End If
End While

Catch ex As Exception
' Trap the error, if any.
'Response.Write("Error : " & ex.Message)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
End If
End Try

But when using this, the SWF file doesn't play.

I think it's something to do with the buffer - perhaps sending too many
bytes to the download, which then somehow corrupts the file - but I'd be
grateful if anyone could help me sort it.

Thank you,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top