将两个字节数组传递给asp.net web api post方法

时间:2017-12-13 22:11:13

标签: c# arrays angularjs asp.net-web-api

我在angularjs应用程序中有两个字节数组。我想将两个字节数组作为post操作传递给body中的api端点。

       function getPayload(byteArray1, byteArray2) {
                return {
                    "byteArrayInfo1": byteArray1,
                    "byteArrayInfo2": byteArray2
                };
            }

        var executePostRequest = function (command, byteArray1, byteArray2) {
                    return $http({
                        method: 'POST',
                        headers: { 'Content-Type': 'application/octet-stream' },
                        data: getPayload(byteArray1,byteArray2),
                        url: env.endPointBasePath + command
                    });
            };

当我调试dot net api端点时,它没有得到两个字节的数组。我尝试将内容类型更改为'内容类型'应用程序/ json'。它不起作用。

我现在关注的是,尝试在一次调用后传递两个字节数组是否合适?这是好习惯吗?如果在javascript / angularjs中有技术合并两个字节数组并在app api端点分割,我会很高兴。

App api终点是基于自我提升的应用程序。我试图从上下文请求体中检索正文并将其映射到ByteArrayPayload类。

private static ByteArrayPayload ExtractContent(OwinContext context)
    {
        if (ContentLengthIsNullOrZero(context.Request.Headers.ContentLength))
        {
            return null;
        }

        int contentLength = Int32.Parse(context.Request.Headers.ContentLength);
        var content = context.Request.Body.Read(contentLength);
       //map content to ByteArrayPayload 
       return new ByteArrayPayload ();
    }

  public class ByteArrayPayload{
    public byte[] ByteArrayInfo1 {get;set;}
    public byte[] ByteArrayInfo2 {get;set;}
  }

1 个答案:

答案 0 :(得分:0)

发送二进制内容的最佳方式是使用ArrayBufferViewBlobsFormData API可用于发送多个二进制项,例如Content-Security-Policy,但效率低,因为base64编码会增加33%的额外开销。

有关详细信息,请参阅MDN Web API Reference - XHR Send Method