{"id":3028,"date":"2019-12-11T08:59:04","date_gmt":"2019-12-11T07:59:04","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3028"},"modified":"2019-12-11T08:59:05","modified_gmt":"2019-12-11T07:59:05","slug":"multipart-formdata","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/multipart-formdata\/","title":{"rendered":"Multipart\/formdata"},"content":{"rendered":"\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/mvc\/models\/file-uploads?view=aspnetcore-3.1\">https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/mvc\/models\/file-uploads?view=aspnetcore-3.1<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using Microsoft.Net.Http.Headers;\nusing System;\nusing System.IO;\n\nnamespace WebApi.Helpers\n{\n    public static class MultipartRequestHelper\n    {\n        \/\/ Content-Type: multipart\/form-data; boundary=\"----WebKitFormBoundarymx2fSWqWSd0OxQqq\"\n        \/\/ The spec at https:\/\/tools.ietf.org\/html\/rfc2046#section-5.1 states that 70 characters is a reasonable limit.\n        public static string GetBoundary(MediaTypeHeaderValue contentType, int lengthLimit)\n        {\n            var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary).Value;\n            if (string.IsNullOrWhiteSpace(boundary))\n                throw new InvalidDataException(\"Missing content-type boundary.\");\n\n            if (boundary.Length > lengthLimit)\n                throw new InvalidDataException($\"Multipart boundary length limit {lengthLimit} exceeded.\");\n            return boundary;\n        }\n\n        public static bool IsMultipartContentType(string contentType)\n        {\n            return !string.IsNullOrEmpty(contentType)\n                   &amp;&amp; contentType.IndexOf(\"multipart\/\", StringComparison.OrdinalIgnoreCase) >= 0;\n        }\n\n        public static bool HasFormDataContentDisposition(ContentDispositionHeaderValue contentDisposition)\n        {\n            \/\/ Content-Disposition: form-data; name=\"key\";\n            return contentDisposition != null\n                &amp;&amp; contentDisposition.DispositionType.Equals(\"form-data\")\n                &amp;&amp; string.IsNullOrEmpty(contentDisposition.FileName.Value)\n                &amp;&amp; string.IsNullOrEmpty(contentDisposition.FileNameStar.Value);\n        }\n\n        public static bool HasFileContentDisposition(ContentDispositionHeaderValue contentDisposition)\n        {\n            \/\/ Content-Disposition: form-data; name=\"myfile1\"; filename=\"Misc 002.jpg\"\n            return contentDisposition != null\n                &amp;&amp; contentDisposition.DispositionType.Equals(\"form-data\")\n                &amp;&amp; (!string.IsNullOrEmpty(contentDisposition.FileName.Value)\n                    || !string.IsNullOrEmpty(contentDisposition.FileNameStar.Value));\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/mvc\/models\/file-uploads?view=aspnetcore-3.1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3028","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3028"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3028\/revisions"}],"predecessor-version":[{"id":3029,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3028\/revisions\/3029"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}