2

I want to download a file in mvc That size is 200mb

The following error occurs :

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown

my code is :

public FileResult Download(string name)
        {
            var videoFilePath = HostingEnvironment.MapPath("~/VideoFile/" + name + ".mp4");
            //The header information
             var file = new FileInfo(videoFilePath);
            //Check the file exist,  it will be written into the response
            if (file.Exists)
            {
                return File(videoFilePath, System.Net.Mime.MediaTypeNames.Application.Octet,
                    file.Name);
            }
            return null;
        }

The error does not occur when the file size is small

why ?

2 Answers 2

3

Recently I kept getting System.OutOfMemoryException exception while working on a web application with Visual Studio 2013.

The full exception message title was as follow:

System.OutOfMemoryException Exception of type 'System.OutOfMemoryException' was thrown.

To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process.

This error could happen due to a variety of reasons related to memory consumption of the application. Looking at my local machine memory, there was always enought memory available! Then what was the cause?

Turns out, Visual Studio uses IIS Exoress 32 bit version by default whereas 64 bit version is available right out there! So as a work around you go to the following path and make Visual studio to use 64 bit version of IISExpress.

Tools | Options | Projects and solutions | Web Projects => Use 64 bit version of IIS Express for websites and projects

enter image description here

Apparently behind the scene Visual Studio changes a flag in windows registry which the key could be found at this path.

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\WebProjects --> Use64BitIISExpress

Sign up to request clarification or add additional context in comments.

1 Comment

You just saved me another 24 hours of thinking what to do!
1

I believe your MVC 4 project needs to be updated to support 200 mb files and also, IIS needs to be updated to support 200 mb files.

To change it at the IIS level, I think adding this to your web.config file should work. (untested)

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="209715200"/>
        </requestFiltering>
    </security>
</system.webServer>

And to pull from another SO post, add the following to your web.config file

<location path="File">
  <system.web>
    <httpRuntime executionTimeout="60" maxRequestLength="4096" />
  </system.web>
</location>
<location path="Picture">
  <system.web>
    <httpRuntime executionTimeout="60" maxRequestLength="1024" />
  </system.web>
</location>

Where "File" and "Picture" are your controller names.

On that link, the accepted answer also addresses upload timeouts, which might be a problem as well for a 200 mb file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.