c# - 从 .ashx 压缩 Javascript 在浏览器中返回解码错误

标签 c# asp.net gzip

背景

我正在设置一个通用处理程序来:

  • 合并并压缩 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);

正在完成,我尝试删除它们。

我尝试使用不同的编码/选项。

此时我真的不确定如何解决这个问题,因为我不知道错误的来源(编码/压缩/其他)。

如有任何帮助,我们将不胜感激!

我发现的关于该主题的其他资源

最佳答案

这是其中一件事,一旦你解释了你的问题,你就会很快找到答案。

我需要将响应写成二进制。所以修改压缩算法以返回一个字节数组:

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/

相关文章:

c# - 如何通过 BindingNavigator 插入、更新和删除 DataGridView 的记录到数据库

c# - ASP .NET 中 DataBind() 函数的反转是什么

python - python'function'对象没有属性'GzipFile'

linux - dd 磁盘镜像 - 何时使用哪个 conv 开关?

c# - LINQ Any() 和 Single() 与具有空检查的 SingleOrDefault()

c# - 使用反向代理在服务中强制执行 SSL

c# - 显示在文本框、标签后面的菜单。 ASP.NET/C#

javascript - Visual Studio 编译器 'syntax error' 关于 javascript 的警告

c# - 将集合发布到 ASP.NET WEB API

c# - 如何从包含多个 GzipStreams 的文件中读取