我有以下控制器方法:
[HttpPost]
[Route("SomeRoute")]
public byte[] MyMethod([FromBody] string ID)
{
byte[] mybytearray = db.getmybytearray(ID);//working fine,returning proper result.
return mybytearray;
}
Run Code Online (Sandbox Code Playgroud)
现在在调用方法(这也是另一个WebApi方法!)我写的像这样:
private HttpClient client = new HttpClient ();
private HttpResponseMessage response = new HttpResponseMessage ();
byte[] mybytearray = null;
response = client.GetAsync(string.Format("api/ABC/MyMethod/{0}", ID)).Result;
if (response.IsSuccessStatusCode)
{
mybytearray = response.Content.ReadAsByteArrayAsync().Result;//Here is the problem
}
Run Code Online (Sandbox Code Playgroud)
现在,问题是字节数组MyMethod发送的是528字节,但是在这之后ReadAsByteArrayAsync,大小变得更大(706字节)并且这些值也被放大了.
有点打击我的头,任何帮助将不胜感激.
谢谢!
我花了几天时间研究和研究上传/下载byte []的解决方案.我很接近,但有一个问题似乎出现在我的AngularJS代码块中.
SO上有一个类似的问题,但它没有回应.请参阅/sf/ask/1669476581/
以下是在我陈述问题之前设置上下文的一些背景信息.
正在开发SPA以替换Silverlight中开发的现有产品,因此它受现有系统要求的限制.SPA还需要针对广泛的设备(移动设备到桌面设备)和主要操作系统.
在几个在线资源(下面列出)的帮助下,我已经完成了大部分代码.我在下面的Byte Rot链接中使用异步多媒体格式化程序来实现byte [].
http://byterot.blogspot.com/2012/04/aspnet-web-api-series-part-5.html
我的问题是下面显示的Angular客户端代码中的服务器响应不正确.
/**********************服务器代码************************/ 在[FromBody] byte []之后为代码添加了缺失项
public class ItemUploadController : ApiController{
[AcceptVerbs("Post")]
public HttpResponseMessage Upload(string var1, string var2, [FromBody]byte[] item){
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(item);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
/*****************示例客户端代码********************/
我从代码中省略的唯一内容是实际的变量参数. …