如何正确实现 MediaTypeFormatter 来处理“多部分/混合"类型的请求? [英] How does one correctly implement a MediaTypeFormatter to handle requests of type 'multipart/mixed'?

查看:36
本文介绍了如何正确实现 MediaTypeFormatter 来处理“多部分/混合"类型的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个用 ASP.NET Web API 编写的 Web 服务,以接受任意数量的文件作为多部分/混合"请求.辅助方法 mat 如下所示(假设 _clientSystem.Net.Http.HttpClient 的一个实例):

Consider a web service written in ASP.NET Web API to accept any number files as a 'multipart/mixed' request. The helper method mat look as follows (assuming _client is an instance of System.Net.Http.HttpClient):

public T Post<T>(string requestUri, T value, params Stream[] streams)
{
    var requestMessage = new HttpRequestMessage();
    var objectContent = requestMessage.CreateContent(
        value,
        MediaTypeHeaderValue.Parse("application/json"),
        new MediaTypeFormatter[] {new JsonMediaTypeFormatter()},
        new FormatterSelector());

    var content = new MultipartContent();
    content.Add(objectContent);
    foreach (var stream in streams)
    {
        var streamContent = new StreamContent(stream);
        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        streamContent.Headers.ContentDisposition =
            new ContentDispositionHeaderValue("form-data")
            {
                Name = "file",
                FileName = "mystream.doc"
            };
        content.Add(streamContent);
    }

    return _httpClient.PostAsync(requestUri, content)
        .ContinueWith(t => t.Result.Content.ReadAsAsync<T>()).Unwrap().Result;
}

ApiController的子类中接受请求的方法有如下签名:

The method that accepts the request in the subclass of ApiController has a signature as follows:

public HttpResponseMessage Post(HttpRequestMessage request)
{
    /* parse request using MultipartFormDataStreamProvider */
}

理想情况下,我想这样定义它,其中根据Content-Disposition"标头的name"属性从multipart/mixed"内容中提取联系人、源和目标.

Ideally, I'd like to define it like this, where contact, source and target are extracted from the 'multipart/mixed' content based on the 'name' property of the 'Content-Disposition' header.

public HttpResponseMessage Post(Contact contact, Stream source, Stream target)
{
    // process contact, source and target
}

但是,使用我现有的签名,将数据发布到服务器会导致 InvalidOperationException 错误消息:

However, with my existing signature, posting the data to the server results in an InvalidOperationException with an error message of:

没有MediaTypeFormatter"可用于读取类型的对象媒体类型为multipart/mixed"的HttpRequestMessage".

No 'MediaTypeFormatter' is available to read an object of type 'HttpRequestMessage' with the media type 'multipart/mixed'.

Internet 上有许多示例,说明如何使用 ASP.NET Web API 和 HttpClient 发送和接收文件.但是,我还没有找到任何显示如何处理此问题的方法.

There are a number of examples on the internet how to send and receive files using the ASP.NET Web API and HttpClient. However, I have not found any that show how to deal with this problem.

我开始考虑实现自定义 MediaTypeFormatter 并将其注册到全局配置中.然而,虽然在自定义 MediaTypeFormatter 中处理序列化 XML 和 JSON 很容易,但不清楚如何处理几乎可以是任何东西的多部分/混合"请求.

I started looking at implementing a custom MediaTypeFormatter and register it with the global configuration. However, while it is easy to deal with serializing XML and JSON in a custom MediaTypeFormatter, it is unclear how to deal with 'multipart/mixed' requests which can pretty much be anything.

推荐答案

看看这个论坛:http://forums.asp.net/t/1777847.aspx/1?MVC4+Beta+Web+API+and+multipart+form+数据

以下是一段代码(由 imran_ku07 发布),可以帮助您实现自定义格式化程序来处理 multipart/form-data:

Here is a snippet of code (posted by imran_ku07) that might help you implement a custom formatter to handle the multipart/form-data:

public class MultiFormDataMediaTypeFormatter : FormUrlEncodedMediaTypeFormatter
{
    public MultiFormDataMediaTypeFormatter() : base()
    {
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));
    }

    protected override bool CanReadType(Type type)
    {
        return true;
    }

    protected override bool CanWriteType(Type type)
    {
        return false;
    }

    protected override Task<object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext)
    {
        var contents = formatterContext.Request.Content.ReadAsMultipartAsync().Result;
        return Task.Factory.StartNew<object>(() =>
        {
            return new MultiFormKeyValueModel(contents);
        });
    }

    class MultiFormKeyValueModel : IKeyValueModel
    {
        IEnumerable<HttpContent> _contents;
        public MultiFormKeyValueModel(IEnumerable<HttpContent> contents)
        {
            _contents = contents;
        }


        public IEnumerable<string> Keys
        {
            get
            {
                return _contents.Cast<string>();
            }
        }

        public bool TryGetValue(string key, out object value)
        {
            value = _contents.FirstDispositionNameOrDefault(key).ReadAsStringAsync().Result;
            return true;
        }
    }
}

然后您需要将此格式化程序添加到您的应用程序中.如果进行自托管,您可以简单地添加:

You then need to add this formatter to your application. If doing self-host you can simply add it by including:

config.Formatters.Insert(0, new MultiFormDataMediaTypeFormatter());

在实例化 HttpSelfHostServer 类之前.

before instantiating the HttpSelfHostServer class.

-- 编辑 --

要解析二进制流,您需要另一个格式化程序.这是我在我的一个工作项目中用来解析图像的一个.

To parse binary streams you'll need another formatter. Here is one that I am using to parse images in one of my work projects.

class JpegFormatter : MediaTypeFormatter
{
    protected override bool CanReadType(Type type)
    {
        return (type == typeof(Binary));
    }

    protected override bool CanWriteType(Type type)
    {
        return false;
    }

    public JpegFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpeg"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpg"));
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/png"));
    }

    protected override Task<object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext)
    {
        return Task.Factory.StartNew(() =>
            {
                byte[] fileBytes = new byte[stream.Length];
                stream.Read(fileBytes, 0, (int)fileBytes.Length);

               return (object)new Binary(fileBytes);
            }); 
    }

    protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
    {
        throw new NotImplementedException();
    }
}

在您的控制器/动作中,您需要执行以下操作:

In your controller/action you'll want to do something along the lines of:

public HttpResponseMessage UploadImage(Binary File) {
 //do something with your file
}

这篇关于如何正确实现 MediaTypeFormatter 来处理“多部分/混合"类型的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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