Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:27:49+00:00 2026-05-26T20:27:49+00:00

I created a script in ASP.NET which list the files from specified folder. protected

  • 0

I created a script in ASP.NET which list the files from specified folder.

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( "<br/>" ) );
         }
      }

In downloading.aspx file, I have :

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

      /// <summary>
      /// Download specified file and his url
      /// </summary>
      /// <param name="filename"></param>
      /// <param name="fileUrl"></param>
      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();
      }

And the DownloadIt method is defined:

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;
      }

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….

That DXF file has 5MB or over but there is 14 bytes

Why ?

Other small files, the size appears correctly.

Have I to do with Stream buffer ?

I solved problem with the following 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 ) ) > 0 ) {
                  response.OutputStream.Write( buffer, offset, count );
               }
               bufferSize = 0;
            }
         } catch {
            return false;
         }

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:27:50+00:00Added an answer on May 26, 2026 at 8:27 pm

    Try,

    byte []bytes=System.IO.File.ReadAllBytes(fileUrl);
    response.Clear();
    response.ClearHeaders();
    response.AddHeader("Content-Type","application/octet-stream");
    response.AddHeader("Content-Length",bytes.Length.ToString());
    response.AddHeader( "Content-Disposition", "attachment; filename=" + filename );
    response.BinaryWrite(bytes);
    response.Flush();
    response.End();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read that aspnet_client folder contain script which is used by asp .net website
I need to read the location of the Temporary ASP.NET Files folder from VBScript
I've created a custom WSS 3.0 application which uses ASP.NET for AJAX The problem
I have created a project with a simple RDLC report in ASP.NET which when
I have a simple ASP.Net MVC View which contains an FCKeditor text box (created
I've created a web site using Asp.Net and the Razor view engine (which is
I'm using ASP.Net and whenever I create a script file or try to include
I created a script that list the directories in the current directory <?php $dir
I have created a script that saves some files in the Documents directory of
I would like to create an ASP.NET MVC web application which has extensible logic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.