【问题标题】:Passing base64 byte array of image in json to webApi将json中的base64字节图像数组传递给webApi
【发布时间】:2014-07-19 06:28:34
【问题描述】:

我已将通过 jQuery 上传的图像转换为 base64 字节数组/字符串。

借助代码:

function readImage(input) {
        debugger;
        if (input.files && input.files[0]) {
            var FR = new FileReader();
            var a = null;
            FR.onload = function (e) {
                array = e.target.result;
            };
            FR.readAsDataURL(input.files[0]);
   }

我现在通过 json 将数组传递到我的 web Api 并遇到问题。

我的 json 调用是这样的:

 $.getJSON('api/TestImage' + '/' + JSON.stringify(array))
            .done(function (data) {
                //doSomething
            });

我的行动是:

       public IHttpActionResult TestImage(String id) 
        {
            return Ok(id);
        }

那么任何人都可以说出问题所在以及需要做什么。

提前谢谢..

【问题讨论】:

  • 执行该操作的控制器的名称是什么?您在 API 调用中缺少控制器名称,即 api/controller/action。当然,假设您使用的是默认路由定义。
  • TestImage 是控制器名称,我猜在 api 调用中没有强制执行操作。 WebApiConfig 管理它。因为它正在管理我的其余操作。
  • 要么将上面的操作重命名为 Get(string id),要么将 jQuery 中的 HTTP 请求 URL 更改为 'api/TestImage/TestImage'

标签: c# javascript json asp.net-web-api2


【解决方案1】:

为什么要在 URL 和 GET 中发送图像?您应该在请求正文中使用 POST 发送它。 base64 中的图像可能非常大,您可能会遇到一些 url 限制。看this

【讨论】:

    猜你喜欢
    • 2012-12-24
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多