背景
我正在设置一个通用处理程序来:
- 合并并压缩 Javascript 和 CSS 文件
- 缓存一个 GZip 版本和一个非 GZip 版本
- 根据请求提供适当的版本
我在 MonoDevelop 工作OSX 10.7.2 上的 v2.8.2
问题
因为我想缓存 GZipped 版本,所以我需要在不使用响应过滤器的情况下进行 GZip
使用 this代码,我可以在服务器上成功压缩和解压缩一个字符串,但是当我将它提供给客户端时,我得到:
- 错误 330 (net::ERR_CONTENT_DECODING_FAILED):未知错误。 ( Chrome )
- 无法解码原始数据 (Safari)
- 无法显示您尝试查看的页面,因为它使用了无效或不受支持的压缩形式。 (火狐)
相关代码
string sCompiled =null;
if(bCanGZip)
{
context.Response.AddHeader("Content-Encoding", "gzip");
bHasValue = CurrentCache.CompiledScripts.TryGetValue(context.Request.Url.ToString() + "GZIP", out sCompiled);
}
//...
//Process files if bHasVale is false
//Compress result of file concatination/minification
//Compression method
public static string CompressString(string text)
{
UTF8Encoding encoding = new UTF8Encoding(false);
byte[] buffer = encoding.GetBytes(text);
using(MemoryStream memoryStream = new MemoryStream()){
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
byte[] compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
}
//...
//Return value
switch(Type){
case FileType.CSS:
context.Response.ContentType = "text/css";
break;
case FileType.JS:
context.Response.ContentType = "application/javascript";
break;
}
context.Response.AddHeader("Content-Length", sCompiled.Length.ToString());
context.Response.Clear();
context.Response.Write(sCompiled);
尝试解决
因为我不确定行是什么:
byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
正在完成,我尝试删除它们。
我尝试使用不同的编码/选项。
此时我真的不确定如何解决这个问题,因为我不知道错误的来源(编码/压缩/其他)。
如有任何帮助,我们将不胜感激!
我发现的关于该主题的其他资源
- http://beta.blogs.microsoft.co.il/blogs/mneiter/archive/2009/03/24/how-to-compress-and-decompress-using-gzipstream-object.aspx
- http://madskristensen.net/post/Compress-and-decompress-strings-in-C.aspx
- http://www.codeproject.com/KB/files/GZipStream.aspx
- http://www.codeproject.com/KB/aspnet/HttpCombine.aspx
- http://webreflection.blogspot.com/2009/01/quick-tip-c-gzip-content.html
- http://www.dominicpettifer.co.uk/Blog/17/gzip-compress-your-websites-html-css-script-in-code
最佳答案
这是其中一件事,一旦你解释了你的问题,你就会很快找到答案。
我需要将响应写成二进制。所以修改压缩算法以返回一个字节数组:
public static byte[] CompressStringToArray(string text){
UTF8Encoding encoding = new UTF8Encoding(false);
byte[] buffer = encoding.GetBytes(text);
using(MemoryStream memoryStream = new MemoryStream()){
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
byte[] compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
return compressedData;
}
}
然后调用:
//Writes a byte buffer without encoding the response stream
context.Response.BinaryWrite(GZipTools.CompressStringToArray(sCompiled));
解决了这个问题。希望这可以帮助其他将面临同样问题的人。
关于c# - 从 .ashx 压缩 Javascript 在浏览器中返回解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8107145/