1

我使用以下代码下载asp.net网站中的文件夹是

 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);

你能给我一个解决方案吗

等待您的回复....

4

2 回答 2

1

您可以尝试使用此代码

TextWriter sw = new StringWriter();
HttpResponse Response = new HttpResponse(sw);
于 2012-10-12T13:23:59.580 回答
1

我将结构替换为

   HttpResponse response = HttpContext.Current.Response;

它工作正常......

谢谢大家的支持

于 2012-10-15T11:20:23.250 回答