2012-01-19 8 views
1

私は、既知のリモートファイルを取得してブラウザ経由で訪問者に返す必要があるASP.NET(VB.NET)アプリケーションを作成しています。ここにあるMicrosoftのサンプルを使用しようとしています:http://support.microsoft.com/?kbid=812406と「このストリームはシーク操作をサポートしていません」というエラーが発生しています。私はどのように進めるか分からない。ftpサーバからブラウザにファイルをダウンロードする

ここにエラー行が記されたコードがあります。

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 

答えて

0

dataToReadで気にしないでください。 lengthが0になるまで読み取りを続けます(つまり、streamer.Read()は0を返しました)。これはストリームの終わりに達したことを意味します。

私のVBは少し錆びですが、私はループがこのようなものになるはずだと思う:魔法のように動作

finished = False 
While Not finished ' Read the bytes. 
    If Response.IsClientConnected Then ' Verify that the client is connected. 
     length = streamer.Read(buffer, 0, 10000) ' Read the data in buffer 
     If length > 0 Then 
      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 
     Else 
      finished = True 
     End If 
    Else 
     finished = True 
    End If 
End While 
+0

を。どうもありがとうございました! –

関連する問題