2012-01-19 6 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

. 고마워요! –

관련 문제