Parsing/Consuming a MultipartFormDataContent (set by MVC) on the WebApi side

前端 未结 1 1097
野性不改
野性不改 2020-12-22 04:24

I got the ByteArrayContent to work

/* MVC Method , ByteArrayContent */

        private async Task ExecuteProxy(string url)
               


        
1条回答
  •  春和景丽
    2020-12-22 05:16

    So the magic method seems to be ReadAsMultipartAsync

    There were two issues with this. One minor, one significant.

    1. ReadAsMultipartAsync is an extension method. (This is the minor issue)

      /* System.Net.Http.Formatting.dll */

    and

    using System.Net.Http.Headers;
    

    This is why I did not see this method originally in the intellisense. (I had not added the reference .. or the using statement to my .cs)

    1. There was an issue with the method itself (outlined in the link below)

    Request.Content.ReadAsMultipartAsync never returns

    So below is what I came up with that allows "by name" finding.

     /* WebApi Delegating Handler , MultipartFormDataContent*/
    
        protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
    
            /* see https://stackoverflow.com/questions/15201255/request-content-readasmultipartasync-never-returns */
            IEnumerable contents = null;
            Task.Factory.StartNew(
                    () =>
                    contents = request.Content.ReadAsMultipartAsync().Result.Contents,
                    CancellationToken.None,
                    TaskCreationOptions.LongRunning, // guarantees separate thread
                    TaskScheduler.Default)
                .Wait();
    
            if (null != contents)
            {
                //// This could be accomplished with LINQ queries, but I've left the for-loops in ... so its easier to see what's going on
                foreach (HttpContent currentHttpContent in contents)
                {
                    if (null != currentHttpContent)
                    {
                        if (null != currentHttpContent.Headers)
                        {
                            HttpContentHeaders cheaders = currentHttpContent.Headers;
    
                            if (null != cheaders)
                            {
                                if (null != cheaders.ContentDisposition)
                                {
                                    System.Net.Http.Headers.ContentDispositionHeaderValue cdhv = cheaders.ContentDisposition;
                                    if (null != cdhv)
                                    {
                                        if (!string.IsNullOrEmpty(cdhv.Name))
                                        {
                                            if (cdhv.Name.Equals("MyByteArrayContent1", StringComparison.OrdinalIgnoreCase))
                                            {
                                                byte[] byteArray = null;
                                                ////currentHttpContent.LoadIntoBufferAsync().Wait();
                                                ////currentHttpContent.ReadAsByteArrayAsync().ContinueWith(t =>
                                                ////{
                                                ////    byteArray = t.Result;
                                                ////});
                                                byteArray = currentHttpContent.ReadAsByteArrayAsync().Result;
                                            }
    
                                            /* you can also check for MyByteArrayContent2, StringContent1Value, StringContent2Value as well, left out for brevity */
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    

    Other links:

    Request.Content.ReadAsMultipartAsync never returns

    Multipart form POST using ASP.Net Web API

    How to get MultipartFormDataContent in Web.API Post method?

    How do I get the file contents of a MultipartMemoryStreamProvider as a byte array?

    Post byte array to Web API server using HttpClient

    Why is the body of a Web API request read once?

    (No answer for the below one)

    How to parse MultipartFormDataContent

    APPEND:

    Here is the linq query I figured out:

            if (null != contents)
            {
                /* sometimes the ContentDisposition.Name is null so the extra where filters are helpful to avoid object-null-reference exception */
                HttpContent foundContent = (from cnt in contents
                                           where null!= cnt && null != cnt.Headers && null != cnt.Headers.ContentDisposition && !string.IsNullOrEmpty(cnt.Headers.ContentDisposition.Name) && cnt.Headers.ContentDisposition.Name.Equals("MyByteArrayContent1", StringComparison.OrdinalIgnoreCase)
                                           select cnt).FirstOrDefault();
    
                if (null != foundContent )
                {
                    byte[] byteArray = foundContent .ReadAsByteArrayAsync().Result;
                }
            }
    

    0 讨论(0)
提交回复
热议问题