Alex
Alex

Reputation: 53

Post and read binary data in ASP.NET MVC

I have a problem with posting of binary data to server via HttpWebRequest. Here is my client code:

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();
}

And then I want to work with request via MVC controller, here is it's signature:

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

But I have error here, saying that data is not Base64 coded array, what am I doing wrong?

Upvotes: 5

Views: 3959

Answers (1)

SLaks
SLaks

Reputation: 888223

You need to escape the + characters in your Base64 by calling Uri.EscapeDataString.

Upvotes: 3

Related Questions