如何在MVC应用程序中增加最大下载大小? [英] How do I increase maximum download size in an MVC application?

查看:88
本文介绍了如何在MVC应用程序中增加最大下载大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

坦率地说,我很沮丧.这超出了我的经验.

I am frankly stumped. This is beyond my experience.

我有一个C#MVC程序,该程序在MemoryStream中生成一个zip文件供下载.该动作方法是通过单击JavaScript的按钮来调用的.

I have a C# MVC program that generates a zip file in a MemoryStream for downloading. The action method is called by a button click to JavaScript.

唯一的问题是,在某些情况下,潜在的文件大小很容易超过一个Gig,据我的阅读,这是一个常见问题.我尝试在IIS(IIS8)上的请求筛选中将最大允许内容长度"增加到3000000000.我尝试将requestLimits maxAllowedContentLength添加到我的web.config中. 我什至尝试通过多次调用action方法来分解zip文件(没有成功),尽管我还没有得到任何确认/否认,甚至有可能.

The only problem is that in some cases the potential file size can easily exceed one Gig and from my reading, that is a common problem. I've tried upping the Maximum Allowed Content Length to 3000000000 in Request Filtering on IIS (IIS8). I've tried adding requestLimits maxAllowedContentLength to my web.config. I've even tried breaking up the zip through multiple calls to the action method (without success), although I have yet to get any confirmation/denial that this is even possible.

IIS或我的web.config中是否有任何我可以忽略的设置?难道这是公司网络问题,无法在应用程序开发人员一级解决?

Is there any setting within IIS or my web.config that I could be overlooking? Could this be a company network issue, not solvable on an app developer's level?

推荐答案

好的,所以用400个字符或更少的字符来解释大型概念有点困难,所以我认为这只会在注释部分引起更多混乱.此外,我认为我们很可能会在此处接近答案".

Okay, so it's kind of hard to explain big concepts in 400 characters or less, so I think I'm just causing more confusion sticking in the comments section. Besides, I think we're close enough here to an "answer" as you're likely to get.

MemoryStream的默认构造函数实际上将初始大小设置为0.实际上,初始大小设置为256左右,但是由于初始大小主要是参考,因此实际上并没有声明该空间.直到需要为止,它从0开始.

The default constructor of MemoryStream essentially sets the initial size to 0. In reality, the initial size is set to somewhere around 256, but since the initial size is mostly a guide, and it doesn't actually claim that space until its needed, it starts at 0.

每次写入流时,它都会检查正在写入的数据量与缓冲区数组的剩余大小.如果不适合写入,它将创建一个更大的新缓冲区数组,并将旧缓冲区数组复制到该缓冲区数组中.这样,设置初始大小 会有所帮助,因为您可以从更大的初始缓冲区数组开始,而不必增加该缓冲区.您可能有更大的机会获得连续的内存块,我将在稍后对此进行解释,但这实际上也不利于您.如果您仅需要1MB的文件,但是要使用100MB的进行初始化,并且没有100MB的连续内存,即使可能有1MB的连续内存,您也会得到OutOfMemoryException

Each time you write to the stream, it checks how much is being written versus the remaining size of the buffer array. If it can't fit the write, it creates a new, larger buffer array and copies the old buffer array into that. In this way, setting an initial size can help somewhat, in that you start off with a larger initial buffer array and you may not need to grow that buffer. You might have a better chance of getting a contiguous block of memory, which I'll explain the importance of in a bit, but that actually kind of works against you, as well. If you only need 1MB for the file, but you're initializing with 100MB and there's not 100MB of contiguous memory, you'll get an OutOfMemoryException, even though there might be 1MB of contiguous memory available.

无论是否初始化,仍然存在某些不变的事实.首先,MemoryStream需要连续的内存.即使从技术上来说,系统上有可用的内存,也可能没有大块可用内存.换句话说,如果您有4GB的可用空间,但它们都分散了,即使试图在内存中创建1GB的流也可能会失败,仅因为它无法保留1GB的连续内存.显然,要在内存中创建的文件越大,遇到此问题的机会就越大. 仅出于这个原因,我会说您很幸运,而没有增加系统RAM的容量. IIS实际上有8GB的空间,可能只有4-6GB的空间,然后在工作进程和线程之间分配,因此您不太可能将25%左右的可用RAM声明为连续空间.

Regardless of whether you initialize or not, there remains certain immutable facts. First, MemoryStream requires contiguous memory. Even if you technically have memory available on the system, it's possible you might not have large blocks of available memory. In other words if you have 4GB available, but it's all fragmented, even trying to create a 1GB stream in memory could fail, simply because it can't reserve 1GB of contiguous memory. Obviously, the larger the file you're tying to create in memory, the greater the chances that you're going to run into this issue. For this reason alone, I would say you're out of luck without raising the amount of system RAM. With 8GB and probably only 4-6GB actually available to IIS and then split up between worker processes and threads, the odds that you're going to be able to claim 25% or so of the available RAM as contiguous space, is highly unlikely.

下一个不变的事实可能相关也可能不相关,但是由于您未指定,因此我将提及它.如果您的Web应用程序部署为32位,则 any 对象的硬限制为2GB,这意味着MemoryStream永远不能容纳超过2GB(实际上大约为1.3-1.6GB). .NET代码会占用一些地址空间),即使您在系统上有一些可笑的RAM(例如1TB +),尝试这样做也会导致OutOfMemoryException.如果您的应用程序是64位的,那么在假定正确编译的情况下,您可以解决更多的内存问题,因此不太可能出现此问题.但是,您将不得不尝试将其弄糟,所以您应该没事.

The next immutable fact may or may not be relevant, but since you haven't specified, I'll mention it. If your web app is deployed as 32-bit, you'll have a hard limit of 2GB for any object, meaning a MemoryStream could never house more than 2GB (actually around 1.3-1.6GB as .NET code consumes some of that address space), and any attempt to make it do so will result in an OutOfMemoryException, even if you had some ridiculous amount of RAM on the system like 1TB+. If your app is 64-bit, this is less likely an issue as you can address a ton more memory, assuming it's compiled properly. You'd have to pretty much try to screw that up, though, so you should be fine.

最后,多次写入也会引起问题.如前所述,缓冲区阵列会根据写入情况调整大小(如果需要).每次调整大小时,新的缓冲区数组还必须 能够容纳在连续的地址空间中.结果,如果您从一开始就写入了所有数据,那么多次调整大小可能会导致您碰到一个不会被击中的OutOfMemoryException.这是初始化MemoryStream可能会有所帮助的地方,但是正如我之前说的,它也是一把双刃剑,因为您的初始缓冲区大小可能太大而无法开始,并且最终会出现异常,而您可能没有有一个让它有机地生长.长短不一,请尝试将所有内容一次性写入流中.

Finally, multiple writes can cause an issue as well. As I said previously, the buffer array resizes (if necessary) in response to writes. Each time it resizes, the new buffer array must also be able to fit in contiguous address space. As a result, multiple resizes can cause you to bump into an OutOfMemoryException you wouldn't have hit if you had written all the data from the start. This is where initializing the MemoryStream can be helpful, but as I said before, it's also a double-edged sword, as your initial buffer size might be too great to begin with and you end up with an exception where you may have not had one letting it grow organically. Long and short, try to write everything to the stream in one go rather than piecemeal.

这篇关于如何在MVC应用程序中增加最大下载大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆