I am creating a ASP.NET (VB.NET) application that must retrieve a known remote file and return it to the visitor through their browser. I am attempting to use the sample from Microsoft located here: http://support.microsoft.com/?kbid=812406 and running into an error ‘This stream does not support seek operations’. I’m not sure how to proceed.
Here is the code with the error line marked.
Dim ftpWebReq As Net.FtpWebRequest = CType(Net.WebRequest.Create(path), Net.FtpWebRequest)
ftpWebReq.Method = Net.WebRequestMethods.Ftp.DownloadFile
ftpWebReq.KeepAlive = False
ftpWebReq.UsePassive = False
ftpWebReq.Credentials = New Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings("FtpId"), System.Web.Configuration.WebConfigurationManager.AppSettings("FtpPwd"))
Dim ftpWebResp As Net.FtpWebResponse = CType(ftpWebReq.GetResponse(), Net.FtpWebResponse)
Dim streamer As Stream = ftpWebResp.GetResponseStream()
Dim buffer(10000) As Byte ' Buffer to read 10K bytes in chunk:
Dim length As Integer ' Length of the file:
Dim dataToRead As Long ' Total bytes to read:
dataToRead = streamer.Length ' *** This is the error line ***
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=foo.txt")
While dataToRead > 0 ' Read the bytes.
If Response.IsClientConnected Then ' Verify that the client is connected.
length = streamer.Read(buffer, 0, 10000) ' Read the data in buffer
Response.OutputStream.Write(buffer, 0, length) ' Write the data to the current output stream.
Response.Flush() ' Flush the data to the HTML output.
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
dataToRead = -1 'prevent infinite loop if user disconnects
End If
End While
Don’t bother with
dataToRead. Keep reading untillengthis 0 (i.estreamer.Read()has returned 0). This means you’ve reached the end of the stream.My VB is a bit rusty, but I think the loop should look something like this: