title: Better way to download a binary file
body: <p>I have a site where a user can download a file.  Some files are extremely large (the largest being 323 MB).  When I test it to try and download this file I get an out-of-memory exception.  The only way I know to download the file is below.  The reason I'm using the code below is that the URL is encoded and I can't let the user link directly to the file.  Is there another way to download this file without having to read the whole thing into a byte array?</p>
  <pre><code>  FileStream fs = new FileStream(context.Server.MapPath(url),
                                   FileMode.Open,
                                   FileAccess.Read);
  
    BinaryReader br = new BinaryReader(fs);
    long numBytes = new FileInfo(context.Server.MapPath(url)).Length;
    byte[] bytes = br.ReadBytes((int) numBytes);
  
    string filename = Path.GetFileName(url);
    context.Response.Buffer = true;
    context.Response.Charset = &quot;&quot;;
  
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.ContentType = &quot;application/x-rar-compressed&quot;;
    context.Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=&quot; + filename);
  
    context.Response.BinaryWrite(bytes);
    context.Response.Flush();
    context.Response.End();
  </code></pre>