从后处理中的Web API二进制数据或PUT REST请求 [英] Processing binary data in Web API from a POST or PUT REST request

查看:328
本文介绍了从后处理中的Web API二进制数据或PUT REST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 REST 使用Web服务网络API 。我曾经遇到过一个问题处理的二进制数据(图像)已通过一个POST请求被发送。

从客户端的角度看,我已成功地发送二进制数据使用的jQuery表单插件即可。但是,因为我很新的.NET(我是一个PHP开发人员),我有困难处理在服务器上通过Web API这个二进制数据。

要确认的jQuery插件需要正确地发送图像数据,我已经写了工作的 PHP 的处理程序,它利用了简单的 $ _ FILE 全局变量。

现在我试图完成通过Web API相同。下面是我所尝试过的轮廓。我如何访问已发送的二进制数据?

型号:

 命名空间EDHDelivery.Models
{
    公共类Oferta
    {
        公众诠释OfertaID {搞定;组; }
        公共字符串农布雷{搞定;组; }
        公共字符串imagen画质{搞定;组; }
        公众诠释ComercioID {搞定;组; }
    }
}

控制器(局部$ C $显示C):

 公共Oferta添加(Oferta项)
{
    / *在这里我的项目将有POST体形式的价值,
    由框架自动序列化,我认为图像二值* /
    变种N = item.Nombre; //...等等。
}


解决方案

在总之,你必须将数据发送为的multipart / form-data的(我'的,米pretty肯定的是,你已经通过你所提到的插件做),然后您必须提取使用Web API MultipartContent 供应商之一的数据。

有丰富的资源,解释如何是:

I'm currently developing a REST web service using Web API. I have encountered a problem processing binary data (an image) that has been transmitted via a POST request.

From the perspective of the client, I have managed to send binary data using the jQuery Form Plugin. But because I'm very new to .NET (I'm a PHP developer), I'm having difficulty processing this binary data via Web API on the server.

To confirm that the jQuery Form Plugin is sending the image data correctly, I have written a working PHP handler that makes use of the simple $_FILE global variable.

Now I am trying to accomplish the same via Web API. Here is an outline of what I have tried. How do I access the binary data that has been sent?

Model:

namespace EDHDelivery.Models
{
    public class Oferta
    {
        public int OfertaID { get; set; }
        public string Nombre { get; set; }
        public string Imagen { get; set; }
        public int ComercioID { get; set; }
    }
}

Controller (partial code shown):

public Oferta Add(Oferta item)
{
    /*here my item will have the POST body with form values, 
    automatically serialized by the framework and I think an image binary*/
    var n = item.Nombre; //...etc.
}

解决方案

In short, you have to send the data as multipart/form-data (which, I'm pretty sure, you are already doing through the plugin you mentioned) and then you have to extract that data using one of Web API MultipartContent providers.

There are plenty of resources explaining how to that:

这篇关于从后处理中的Web API二进制数据或PUT REST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆