23

考虑一个用 ASP.NET Web API 编写的 Web 服务,以接受任何数量的文件作为“多部分/混合”请求。辅助方法 mat 如下所示(假设_client是 的一个实例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 子类中接受请求的方法有如下签名:

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

理想情况下,我想这样定义它,其中联系人、源和目标是根据“Content-Disposition”标题的“名称”属性从“多部分/混合”内容中提取的。

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

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

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

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

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

4

2 回答 2

15

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

这是一段代码(由 imran_ku07 发布),它可以帮助您实现自定义格式化程序来处理多部分/表单数据:

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;
        }
    }
}

然后,您需要将此格式化程序添加到您的应用程序中。如果做自托管,您可以简单地添加它,包括:

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

在实例化 HttpSelfHostServer 类之前。

- 编辑 -

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

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();
    }
}

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

public HttpResponseMessage UploadImage(Binary File) {
 //do something with your file
}
于 2012-04-06T04:23:41.867 回答
2

看看这篇文章https://stackoverflow.com/a/17073113/1944993 Kiran Challa 的回答真的很好!

必不可少的部分:

自定义内存 MultiaprtFormDataStreamProvider:

    public class InMemoryMultipartFormDataStreamProvider : MultipartStreamProvider
{
    private NameValueCollection _formData = new NameValueCollection();
    private List<HttpContent> _fileContents = new List<HttpContent>();

    // Set of indexes of which HttpContents we designate as form data
    private Collection<bool> _isFormData = new Collection<bool>();

    /// <summary>
    /// Gets a <see cref="NameValueCollection"/> of form data passed as part of the multipart form data.
    /// </summary>
    public NameValueCollection FormData
    {
        get { return _formData; }
    }

    /// <summary>
    /// Gets list of <see cref="HttpContent"/>s which contain uploaded files as in-memory representation.
    /// </summary>
    public List<HttpContent> Files
    {
        get { return _fileContents; }
    }

    public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
    {
        // For form data, Content-Disposition header is a requirement
        ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
        if (contentDisposition != null)
        {
            // We will post process this as form data
            _isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));

            return new MemoryStream();
        }

        // If no Content-Disposition header was present.
        throw new InvalidOperationException(string.Format("Did not find required '{0}' header field in MIME multipart body part..", "Content-Disposition"));
    }

    /// <summary>
    /// Read the non-file contents as form data.
    /// </summary>
    /// <returns></returns>
    public override async Task ExecutePostProcessingAsync()
    {
        // Find instances of non-file HttpContents and read them asynchronously
        // to get the string content and then add that as form data
        for (int index = 0; index < Contents.Count; index++)
        {
            if (_isFormData[index])
            {
                HttpContent formContent = Contents[index];
                // Extract name from Content-Disposition header. We know from earlier that the header is present.
                ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
                string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;

                // Read the contents as string data and add to form data
                string formFieldValue = await formContent.ReadAsStringAsync();
                FormData.Add(formFieldName, formFieldValue);
            }
            else
            {
                _fileContents.Add(Contents[index]);
            }
        }
    }

    /// <summary>
    /// Remove bounding quotes on a token if present
    /// </summary>
    /// <param name="token">Token to unquote.</param>
    /// <returns>Unquoted token.</returns>
    private static string UnquoteToken(string token)
    {
        if (String.IsNullOrWhiteSpace(token))
        {
            return token;
        }

        if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
        {
            return token.Substring(1, token.Length - 2);
        }

        return token;
    }}

然后,您可以像这样在 webapi 中使用“MemoryMultiPartDataStreamProvider”:

var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider());

    //access form data
    NameValueCollection formData = provider.FormData;

    //access files
    IList<HttpContent> files = provider.Files;
于 2015-06-24T08:40:46.033 回答