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

发布于 2024-11-24 04:36:34 字数 764 浏览 0 评论 0原文

我在通过 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 编码数组,我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-12-01 04:36:34

您需要通过调用 Uri.EscapeDataString 对 Base64 中的 + 字符进行转义。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文