{"id":1950,"date":"2022-08-30T15:20:48","date_gmt":"2022-08-30T15:20:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/readasmultipartasync-throws-system-argumentexception-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:48","modified_gmt":"2022-08-30T15:20:48","slug":"readasmultipartasync-throws-system-argumentexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/readasmultipartasync-throws-system-argumentexception-collection-of-common-programming-errors\/","title":{"rendered":"ReadAsMultipartAsync Throws System.ArgumentException-Collection of common programming errors"},"content":{"rendered":"<p>I have tried searching on here and Google for an answer to this but have yet to find one. I am using what I have found to be fairly standard for .NET 4.0 upload to Web API service. Here is the code:<\/p>\n<pre><code>    public HttpResponseMessage Post()\n    {\n        if (!Request.Content.IsMimeMultipartContent())\n        {\n            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);\n        }\n        StringBuilder sb = new StringBuilder();\n\n        string root = HttpContext.Current.Server.MapPath(\"~\/App_Data\");\n        var provider = new MyMultipartFormDataStreamProvider(root);\n\n        var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =&gt;\n        {\n            if (t.IsFaulted || t.IsCanceled)\n            {\n                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);\n            }\n\n            \/\/ This will give me the form field data\n            foreach (var key in provider.FormData.AllKeys)\n            {\n                foreach (var val in provider.FormData.GetValues(key))\n                {\n                    sb.Append(string.Format(\"{0}: {1}\", key, val));\n                }\n            }\n\n            \/\/ This will give me any file upload data\n            foreach (MultipartFileData file in provider.FileData)\n            {\n                sb.Append(file.Headers.ContentDisposition.FileName);\n                sb.Append(\"Server file path: \" + file.LocalFileName);\n            }\n            return new HttpResponseMessage()\n            {\n                Content = new StringContent(sb.ToString())\n            };\n        });\n        return Request.CreateResponse(HttpStatusCode.OK);\n    }\n<\/code><\/pre>\n<p>When I create a very basic form with a input type=file and submit it I am getting an exception thrown for files over about 800Kb. Here is the exception:<\/p>\n<pre><code>System.ArgumentException was unhandled by user code\n  HResult=-2147024809\n  Message=Value does not fall within the expected range.\n  Source=mscorlib\n  StackTrace:\n       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\n       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)\n       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)\n       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)\n       at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress()\n       at System.Web.HttpWorkerRequest.IsLocal()\n       at System.Web.Configuration.CustomErrorsSection.CustomErrorsEnabled(HttpRequest request)\n       at System.Web.HttpContextWrapper.get_IsCustomErrorEnabled()\n       at System.Web.Http.WebHost.HttpControllerHandler.c__DisplayClassa.b__9()\n       at System.Lazy`1.CreateValue()\n       at System.Lazy`1.LazyInitValue()\n       at System.Lazy`1.get_Value()\n       at System.Web.Http.HttpConfiguration.ShouldIncludeErrorDetail(HttpRequestMessage request)\n       at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)\n       at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Exception exception)\n       at aocform.Controllers.ValuesController.c__DisplayClass2.b__1(Task`1 t) in c:\\Users\\fred_malone\\Documents\\Visual Studio 2012\\Projects\\aocform\\aocform\\Controllers\\ValuesController.cs:line 30\n       at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()\n       at System.Threading.Tasks.Task.Execute()\n  InnerException: \n<\/code><\/pre>\n<p>I check the App_Data folder and I see part of the file there. This small part is not always the same size either, like maybe it cuts off at a certain size.<\/p>\n<p>I have adjusted both the <code>maxRequestLength<\/code> and the <code>maxAllowedContentLength<\/code> to large numbers with no success.<\/p>\n<p>What does this message mean and what should I be looking at to fix it?<\/p>\n<p>Thanks.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-12-05 16:29:59. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have tried searching on here and Google for an answer to this but have yet to find one. I am using what I have found to be fairly standard for .NET 4.0 upload to Web API service. Here is the code: public HttpResponseMessage Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } StringBuilder sb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1950","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1950"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1950\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}