如何将大量数据传递到我的Web API应用程序?
我在Web API控制器中有这个代码:
[Route("{unit}/{begindate}/{enddate}")] [HttpPost] public void Post(string unit, string begindate, string enddate, [FromBody] string stringifiedjsondata) { List _produceUsageList = JsonConvert.DeserializeObject<List>(stringifiedjsondata); string _unit = unit; string _begindate = String.Format("{0}01", HyphenizeYYYYMM(begindate)); string _enddate = GetEndOfMonthFor(enddate); string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/"); string htmlStr = ConvertProduceUsageListToHtml(_produceUsageList); string htmlFilename = string.Format("produceUsage_{0}_{1}_{2}.html", _unit, _begindate, _enddate); string fullPath = Path.Combine(appDataFolder, htmlFilename); File.WriteAllText(fullPath, htmlStr); }
当通过“[FromBody]”arg从客户端(Winforms应用程序)传递一些(人为的)数据时,它工作正常,如下所示:
private async Task SaveProduceUsageFileOnServer(string beginMonth, string beginYear, string endMonth, string endYear, DataTable _dtUsage) { string beginRange = String.Format("{0}{1}", beginYear, beginMonth); string endRange = String.Format("{0}{1}", endYear, endMonth); HttpClient client = new HttpClient {BaseAddress = new Uri("http://localhost:42176")}; string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]"; //string dataAsJson = JsonConvert.SerializeObject(_dtUsage); String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange); var content = new FormUrlEncodedContent(new[] { new KeyValuePair("", dataAsJson) }); HttpResponseMessage response = await client.PostAsync(uriToCall, content); }
但是,如果我将对dataAsJson的赋值的注释反转,以便它发送实际数据,如下所示:
//string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]"; string dataAsJson = JsonConvert.SerializeObject(_dtUsage);
…它失败; 没有错误的消息; 使用JsonConvert.SerializeObject()方法将DataTable(_dtUsage)序列化为json。 只是有很多数据(几千条记录)。 永远不会到达客户端对PostAsync()的调用,因此永远不会到达服务器的Post方法。
是否有一种解决方法可以在发送超过一小部分数据时成功? 我真的不喜欢通过线路传递那么多数据(目前客户端和服务器都在本地运行,但考虑到部署后的情况),但另一种选择是从客户端执行相同的存储过程(到在那里生成Excel电子表格文件)并从服务器生成(将数据转换为HTML)。 它似乎是那些“Catch-22”情境之一(“如果我这样做就会被扣押,如果我不这样做就会被扣留”)。
UPDATE
测试了user3093073的想法,我将其添加到Web.config的system.webServer部分:
(基于我在这里找到的东西 ,但它仍然无法正常工作……
更新2
更接近用户user3093073的回答,我也试过这个:
<!-- -->
…也无济于事。
更新3
请注意,我将上面的代码放在站点中的每个Web.config文件中,即:
The Web.config file below the \[ProjectName]\Views folder The Web.config file below the \[ProjectName] folder The two Web.config files below the \[ProjectName]\Web.config file, namely "Web.Debug.config" and "Web.Release.config"
…或者,另一种查看其位置的方式:
\PlatypusReports\Web.config \PlatypusReports\Web.config\Web.Debug.config \PlatypusReports\Web.config\Web.Release.config \PlatypusReports\Views\Webconfig
更新4
在离开这几天并回到它之后,我现在似乎很清楚,我试图通过的“真实”数据是与虚假/测试数据不同的动物。
测试数据是一个简单的KeyValuePair的集合。 但实际数据更为复杂。 因此,当我尝试将复杂数据转换为简单的KeyValuePair时,肯定会遇到麻烦。 所以不是这样的:
new KeyValuePair("", dataAsJson)
……我需要这样的事情:
new List("", dataAsJson)
……但那也不起作用; 我得到了几个编译错误,例如, ”System.Collections.Generic.List’不包含带有2个参数的构造函数’ “
如果我删除第一个arg,那么它是:
new List(dataAsJson)
…我得到其他编译错误,例如“ 参数1:无法从’字符串’转换为’int’ ”
我相信既然您已经指定了
设置,那么您还需要设置maxRequestLength
。 正如@brewsky所提到的,这是默认的,它只有4MB。 检查你的web.config。 这是2GB的设置
这个答案似乎确实解决了类似的问题。
您必须设置IIS以接受大文件。 默认情况下,它只有4MB左右。 检查你的web.config。 这是2GB的设置: