Couldn't save larger downloaded file in ASP.NET
<p>I created a script in ASP.NET which list the files from specified folder.</p>

<pre><code>protected void Page_Load( object sender, EventArgs e ) {

         //get file list
         DirectoryInfo di = new DirectoryInfo( Server.MapPath( "~/download" ) );
         int i = 0;
         foreach ( FileInfo fi in di.GetFiles() ) {
            HyperLink link = new HyperLink();
            link.ID = "HyperLink" + ( i++ );
            link.ToolTip = fi.Length.ToString();
            link.Text = fi.Name;
            link.NavigateUrl = "downloading.aspx?file=" + fi.Name;
            Page.Controls.Add( link );
            Page.Controls.Add( new LiteralControl( "&lt;br/&gt;" ) );
         }
      }
</code></pre>

<p>In <code>downloading.aspx</code> file, I have :</p>

<pre><code>protected void Page_Load( object sender, EventArgs e ) {
         string filename = Request["file"].ToString();
         FileDownload( filename, Server.MapPath( "~/download/" + filename ) );
      }

      /// &lt;summary&gt;
      /// Download specified file and his url
      /// &lt;/summary&gt;
      /// &lt;param name="filename"&gt;&lt;/param&gt;
      /// &lt;param name="fileUrl"&gt;&lt;/param&gt;
      private void FileDownload( string filename, string fileUrl ) {
         Page.Response.Buffer = true;
         Page.Response.Clear();
         DownloadFile df = new DownloadFile();
         bool success = df.DownloadIt( Page.Response, filename, fileUrl );
         if ( !success ) {
            Response.Write( "Download error" );
         }
         Page.Response.End();
      }
</code></pre>

<p>And the <code>DownloadIt</code> method is defined:</p>

<pre><code>public bool DownloadIt( HttpResponse response, string filename, string fileUrl ) {

         if ( response == null ) {
            throw new Exception();
         }

         if ( string.IsNullOrEmpty(filename) ) {
            throw new ArgumentNullException( "filename" );
         }

         try {
            response.ContentType = "application/octet-stream";
            response.AppendHeader( "Content-Disposition", "attachment; filename=" + filename );
            response.TransmitFile( fileUrl );
         } catch {
            return false;
         }
         return true;
      }
</code></pre>

<p>In that list in default page, I have a file which has 5-6 MB and when I clicked on it, the dialog browser appears (that with 'Save' 'Open' 'Cancel' buttons) but there is a field where the file size is 14 bytes....</p>

<p><img src="http://i.stack.imgur.com/2pWKq.jpg" alt="That DXF file has 5MB or over but there is 14 bytes"></p>

<p>Why ?</p>

<p>Other small files, the size appears correctly.</p>

<p>Have I to do with Stream buffer ?</p>

<p><strong>I solved problem</strong> with the following code:</p>

<pre><code>try {
            FileInfo fileDownload = new FileInfo( fileUrl );
            int bufferSize = 0;
            bufferSize = (int)fileDownload.Length;
            using ( Stream s = new FileStream( fileUrl, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize) ) {
               byte[] buffer = new byte[bufferSize];
               int count = 0;
               int offset = 0;
               response.ContentType = "application/octet-stream";
               response.AddHeader( "Content-Length", buffer.Length.ToString() );
               response.AddHeader( "Content-Disposition", "attachment; filename=" + filename );
               while ( ( count = s.Read( buffer, offset, buffer.Length ) ) &gt; 0 ) {
                  response.OutputStream.Write( buffer, offset, count );
               }
               bufferSize = 0;
            }
         } catch {
            return false;
         }
</code></pre>

<p>Now the problem becomes for files which have > 200 MB due of <code>bufferSize</code> which is int and it is a right parameter for <code>FileStream</code>.</p>
