title: MVC 4.5.2 Response.WriteFile larger than 2gb
body: <p>In late testing phase i noticed an issue my application has sending files larger than 2 gigs (Int32.MaxValue). Is there an easy way to remedy this ? These are large high def .mov movie files. I used a Response.WriteFile in my controller to ensure the browser doesn't open any of the files (most are large video files that aren't opened by the browser but not all) and force the download as well as do some logging.</p>
  
  <p>Right now my only option is to use ajax to do javascript based logging and link directly to the large mov files but i would like to avoid changing my logging methods so late in the development process. and would have really liked all the links to use one method of logging whether they are large mov files or files that need to be sent using writefile so the browser doesn't open them for simplicity sake. but it seems i have dont have much choice.</p>
  
  <p>What is the logic behind preventing large file transfers like this ? In the stack trace i see everywhere Int64 size not Int32 so why is it not working ?   </p>
  
  <p>I am using latest(?) MVC (4.5.2) and VS2015.</p>
  
  <p>This is the error i get when i request a large file:</p>
  
  <pre><code>[ArgumentOutOfRangeException: The size parameter must be between zero and the maximum Int32 value.
  Parameter name: size
  Actual value was 4563025348.]
     System.Web.HttpFileResponseElement..ctor(String filename, Int64 offset, Int64 size, Boolean isImpersonating, Boolean useTransmitFile, Boolean supportsLongTransmitFile) +3824261
     System.Web.HttpWriter.WriteFile(String filename, Int64 offset, Int64 size) +119
     System.Web.HttpResponse.WriteFile(String filename, Boolean readIntoMemory) +359
     System.Web.HttpResponseWrapper.WriteFile(String filename) +30
  </code></pre>
  
  <p>And this is the relevant part of my controller code</p>
  
  <pre><code>Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
  Response.WriteFile(filePath);
  Response.End();
  </code></pre>
  
  <p>Thank you</p>
  
  <p>N.</p>
  
  <p>Edit:
  Final code looks like this:</p>
  
  <pre><code>    using (FileStream fs = new FileStream(serverPath, FileMode.Open))
          {
              byte[] buffer = new byte[4096];
              int count = 0;
  
              Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
              while ((count = fs.Read(buffer, 0, buffer.Length)) &gt; 0)
              {
                  Response.OutputStream.Write(buffer, 0, count);
                  Response.Flush();
              }
          }
  
  HttpContext.ApplicationInstance.CompleteRequest();
  </code></pre>