locked
having issue with upload multipart/form-data RRS feed

  • Question

  • User-1716951449 posted

    I'm working a method to capture uploading file

    it's slightly different with those sample code googled, this Api accept 1 parameter, an object. within has a byte[] data, which I will fill it with the file .

    when testing, i'm getting The request entity's media type 'multipart/form-data' is not supported for this resource

    so googling hinting me to add this type to global formatter

            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                JsonOnlySupportUTF16();
    
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
    
            void JsonOnlySupportUTF16()
            {
                var jsonSupportedEncodings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedEncodings;
                jsonSupportedEncodings.Clear();
                jsonSupportedEncodings.Add(System.Text.Encoding.Unicode);
    
                GlobalConfiguration.Configuration.Formatters.Add(new UploadMultipartMediaTypeFormatter());
            }
    
        }
    
        public class UploadMultipartMediaTypeFormatter : System.Net.Http.Formatting.MediaTypeFormatter
        {
            public UploadMultipartMediaTypeFormatter()
            {
                SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data"));
            }
    
            public override bool CanReadType(Type type)
            {
                return true;
            }
    
            public override bool CanWriteType(Type type)
            {
                return false;
            }
        }

    initially i return true for CanWriteType, and for unknown reason, my client test app will get 500 error, and no details.

    after changing CanWriteType to false, now I'm getting err:

    The media type formatter of type 'UploadMultipartMediaTypeFormatter' does not support reading because it does not implement the ReadFromStreamAsync method

    Does anyone have any reference to get this done right?

    I'm spending time to tackling this multipart/form-data, but isn't this is a general stuff on web platform, I can't find built-in class of this, do I really need to create this myself?

    And I'm not sure about the Can* property, I just wanna get into the uploading testing.

    Friday, December 11, 2015 11:14 AM

Answers