I have an iPad app that submits orders to an ASP.NET MVC web site via form post. It is posting JSON which can be fairly large for a mobile device to send (200~300K) under certain conditions. I can GZip the form post but then my asp.net mvc chokes on the gzipped content.
我有一个iPad应用程序可以向ASP提交订单。NET MVC网站通过表单发布。它发布的JSON对于移动设备在一定条件下发送(200~300K)是相当大的。我可以GZip表单的帖子,但是我的asp.net mvc会阻塞gziked内容。
How can I handle a GZipped form post in asp.net mvc?
如何在asp.net mvc中处理GZipped表单?
UPDATE:
更新:
Darin's answer puts me on the right track but I still have no idea how to do what he suggests, so here is where I am at:
达林的回答让我走上了正确的道路,但我仍然不知道如何去做他的建议,所以我在这里:
Have this code to decompress a string:
使用以下代码解压缩字符串:
http://dotnet-snippets.com/dns/compress-and-decompress-strings-SID612.aspx
http://dotnet-snippets.com/dns/compress-and-decompress-strings-SID612.aspx
And I get the string like so:
我得到的弦是这样的:
StreamReader reader = new StreamReader(Request.InputStream);
string encodedString = reader.ReadToEnd();
but this gives me the error:
但这给了我一个错误:
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.
EDIT - COMPLETED CODE
编辑完成的代码
I am using asp.net MVC and this is working great for me. I also had to deal with some other encoding that happens when my gzipping occurs:
我正在使用asp.net MVC,这对我来说非常有用。我还必须处理gzipping时发生的其他编码:
[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();
}
}
3 个解决方案
#1
1
You can do this without a custom model binder. Write an Action that accepts HttpPostedFileBase, i.e, treat this as a file upload.
您可以在没有自定义模型绑定器的情况下这样做。编写一个接受HttpPostedFileBase (i)的操作。e,把它当作文件上传。
[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");
}
You are going to need to change your client side code to send a file upload request but that should be fairly easy. For example you can look at this code.
您将需要更改客户端代码以发送文件上传请求,但这应该相当容易。例如,您可以查看此代码。
#2
1
How can I handle a GZipped form post in asp.net mvc?
如何在asp.net mvc中处理GZipped表单?
You could write a custom model binder that will directly read the Request.InputStream, unzip it and then parse the contents and instantiate some view model you want to bind to.
您可以编写一个可以直接读取请求的自定义模型绑定器。InputStream,解压它,然后解析内容并实例化您想要绑定到的某个视图模型。
#3
1
Use the System.IO.Compression.GZipStream class.
使用System.IO.Compression。GZipStream类。
Codeproject上的例子
#1
1
You can do this without a custom model binder. Write an Action that accepts HttpPostedFileBase, i.e, treat this as a file upload.
您可以在没有自定义模型绑定器的情况下这样做。编写一个接受HttpPostedFileBase (i)的操作。e,把它当作文件上传。
[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");
}
You are going to need to change your client side code to send a file upload request but that should be fairly easy. For example you can look at this code.
您将需要更改客户端代码以发送文件上传请求,但这应该相当容易。例如,您可以查看此代码。
#2
1
How can I handle a GZipped form post in asp.net mvc?
如何在asp.net mvc中处理GZipped表单?
You could write a custom model binder that will directly read the Request.InputStream, unzip it and then parse the contents and instantiate some view model you want to bind to.
您可以编写一个可以直接读取请求的自定义模型绑定器。InputStream,解压它,然后解析内容并实例化您想要绑定到的某个视图模型。
#3
1
Use the System.IO.Compression.GZipStream class.
使用System.IO.Compression。GZipStream类。
Codeproject上的例子