【问题标题】:how do I handle and deflate a GZipped form post in asp.net MVC?如何在 asp.net MVC 中处理和放气 GZipped 表单帖子?
【发布时间】:2012-02-14 18:38:10
【问题描述】:

我有一个 iPad 应用程序,它通过表单发布向 ASP.NET MVC 网站提交订单。它发布的 JSON 在某些条件下对于移动设备发送(200~300K)来说可能相当大。我可以对表单发布进行 GZip,但随后我的 asp.net mvc 被 gzip 压缩的内容阻塞了。

如何在 asp.net mvc 中处理 GZipped 表单帖子?

更新:

Darin 的回答让我走上了正确的道路,但我仍然不知道如何按照他的建议去做,所以这就是我所在的位置:

让这个代码解压一个字符串:

http://dotnet-snippets.com/dns/compress-and-decompress-strings-SID612.aspx

我得到这样的字符串:

StreamReader reader = new StreamReader(Request.InputStream);
string encodedString = reader.ReadToEnd();

但这给了我错误:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.

编辑 - 完成代码

我正在使用 asp.net MVC,这对我来说非常有用。我还必须处理 gzip 压缩发生时发生的其他一些编码:

[Authorize]
[HttpPost]
[ValidateInput(false)]
public ActionResult SubmitOrder()
        {

            GZipStream zipStream = new GZipStream(Request.InputStream, CompressionMode.Decompress);
            byte[] streamBytes = ReadAllBytes(zipStream);
            var result = Convert.ToBase64String(streamBytes);
            string sample = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(result));
            string escaped = Uri.UnescapeDataString(sample);

 // escaped now has my form values as a string like so: var1=value1&var2=value2&ect...

//more boring code

}



 public static byte[] ReadAllBytes(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

【问题讨论】:

  • 你如何压缩你的帖子数据?你用javascript实时压缩它?
  • 我在 iOS 应用程序中压缩我的帖子数据,这是我正在使用 ASIHttpRequest 的库中的一个选项:allseeing-i.com/ASIHTTPRequest

标签: asp.net asp.net-mvc gzip


【解决方案1】:

您可以在没有自定义模型绑定器的情况下执行此操作。编写一个接受 HttpPostedFileBase 的 Action,即将其视为文件上传。

    [HttpPost]
    public ActionResult UploadCompressedJSON(HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            GZipStream zipStream = new GZipStream(file.InputStream, CompressionMode.Decompress);
            byte[] streamBytes = ReadAllBytes(zipStream);
            var result = Convert.ToBase64String(streamBytes);
        }
        return RedirectToAction("Index");
    }

您将需要更改客户端代码以发送文件上传请求,但这应该相当容易。例如你可以看看这个code

【讨论】:

  • 我的客户端代码已经在运行,直到我阅读了这个答案,它才会点击我 - 谢谢!我更新了我的问题以包含完整的代码,以防有人需要。
【解决方案2】:

如何在 asp.net mvc 中处理 GZipped 表单帖子?

您可以编写一个自定义模型绑定器,该绑定器将直接读取 Request.InputStream,解压缩它,然后解析内容并实例化您想要绑定到的某些视图模型。

【讨论】:

  • 知道如何正确放气 Request.InputStream 吗?一直把我的头撞到墙上。
【解决方案3】:

使用 System.IO.Compression.GZipStream 类。

Codeproject example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2018-10-16
    • 2017-05-31
    相关资源
    最近更新 更多