在ASP.NET MVC中发布和读取二进制数据

时间:2011-07-14 13:48:14

标签: c# asp.net-mvc binary http-post

我在通过HttpWebRequest向服务器发布二进制数据时遇到问题。这是我的客户代码:

var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.UserAgent = RequestUserAgent;
request.ContentType = "application/x-www-form-urlencoded";
var responseStr = "";
var dataStr = Convert.ToBase64String(data);
var postData = Encoding.UTF8.GetBytes(
string.Format("uploadid={0}&chunknum={1}&data={2}", uploadId, chunkNum, dataStr));
using (var dataStream = request.GetRequestStream())
{
    dataStream.Write(postData, 0, postData.Length);
    dataStream.Close();
}

然后我想通过MVC控制器处理请求,这是它的签名:

public ActionResult UploadChunk(Guid? uploadID, int? chunkNum, byte[] data)

但我在这里有错误,说数据不是Base64编码数组,我做错了什么?

1 个答案:

答案 0 :(得分:3)

您需要通过调用+来转义Base64中的Uri.EscapeDataString个字符。