嗨,我需要从ASHX提供GZ压缩文件。在代码中,我已经清楚了字符串:
public void ProcessRequest(HttpContext context)
{
// this is the code without compression
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "text/xml";
// this is the string to compress and send to the client
string xml = GenerateXml();
Response.Write(output);
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要
有什么帮助吗?
您可以在IIS级别为特定目录启用压缩。我相信这将比在通用处理程序中手动执行更为有效。
更新:
您可以使用GZipStream将xml直接压缩为w响应流:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/x-gzip";
var xml = "<xml/>";
using (var gzipStream = new GZipStream(context.Response.OutputStream, CompressionMode.Compress))
{
var buffer = Encoding.UTF8.GetBytes(xml);
gzipStream.Write(buffer, 0, buffer.Length);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3428 次 |
| 最近记录: |