2012-10-12 56 views
1

發送httpresponses我用下面的代碼來下載文件夾在asp.net網站的庫文件c#

string path = @"E:\sample.zip"; 
     FileInfo file = new FileInfo(path); 
      int len = (int)file.Length, bytes; 
      Response.ContentType = "text/html"; 
      // Response.AddHeader "Content-Disposition", "attachment;filename=" + filename; 
      Response.AppendHeader("content-length", len.ToString()); 
      byte[] buffer = new byte[1024]; 

     using(Stream stream = File.OpenRead(path)) { 
    while (len > 0 && (bytes = 
     stream.Read(buffer, 0, buffer.Length)) > 0) 
    { 
     Response.OutputStream.Write(buffer, 0, bytes); 
     len -= bytes; 
    } 
} 

它工作正常/ ...

但我的問題是,當我使用相同的代碼庫文件作爲

FileInfo file = new FileInfo(ZipPath); 
      int len = (int)file.Length, bytes; 
      HttpResponse Response = new HttpResponse(TextWriter.Null); 
      Response.ContentType = "text/html"; 
      Response.AppendHeader("content-length", len.ToString()); 
      byte[] buffer = new byte[1024]; 

      using (Stream stream = File.OpenRead(ZipPath)) 
      { 
       while (len > 0 && (bytes = 
        stream.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        Response.OutputStream.Write(buffer, 0, bytes); 
        len -= bytes; 
       } 
      } 
     } 

它拋出我誤差

使用自定義TextWriter時,OutputStream不可用。

我想這個問題是在該行

HttpResponse Response = new HttpResponse(TextWriter.Null); 

你能提供給我一個解決方案

等待您的答覆....

回答

1

我將結構替換爲

HttpResponse response = HttpContext.Current.Response; 

它工作正常.....

感謝大家的支持

1

您可以使用此代碼嘗試

TextWriter sw = new StringWriter(); 
HttpResponse Response = new HttpResponse(sw); 
+0

但沒有使用它 – GowthamanSS