Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I download big file of applications

How do I download big file of applications

Scheduled Pinned Locked Moved C#
performancehelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 12016106
    wrote on last edited by
    #1

    I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:

    string Files = ListBox1.SelectedValue;
    byte[] fileBytes = System.IO.File.ReadAllBytes(Files);

            WebClient User = new WebClient();
            byte\[\] FileBuffer = User.DownloadData(Files);
    
            if (FileBuffer != null)
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                context.Response.Clear();
                context.Response.ClearHeaders();
                context.Response.ClearContent();
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("content-length", FileBuffer.Length.ToString());
                context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files));
                context.Response.BinaryWrite(FileBuffer);
                context.ApplicationInstance.CompleteRequest();
    

    Could pls anyone help...

    L Richard DeemingR 2 Replies Last reply
    0
    • M Member 12016106

      I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:

      string Files = ListBox1.SelectedValue;
      byte[] fileBytes = System.IO.File.ReadAllBytes(Files);

              WebClient User = new WebClient();
              byte\[\] FileBuffer = User.DownloadData(Files);
      
              if (FileBuffer != null)
              {
                  System.Web.HttpContext context = System.Web.HttpContext.Current;
                  context.Response.Clear();
                  context.Response.ClearHeaders();
                  context.Response.ClearContent();
                  context.Response.ContentType = "application/octet-stream";
                  context.Response.AddHeader("content-length", FileBuffer.Length.ToString());
                  context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files));
                  context.Response.BinaryWrite(FileBuffer);
                  context.ApplicationInstance.CompleteRequest();
      

      Could pls anyone help...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 12016106 wrote:

      byte[] fileBytes = System.IO.File.ReadAllBytes(Files);

      Do not assume that there is enough memory to hold the entire file.

      M 1 Reply Last reply
      0
      • L Lost User

        Member 12016106 wrote:

        byte[] fileBytes = System.IO.File.ReadAllBytes(Files);

        Do not assume that there is enough memory to hold the entire file.

        M Offline
        M Offline
        Member 12016106
        wrote on last edited by
        #3

        So how should declare it to download large size file??

        M 1 Reply Last reply
        0
        • M Member 12016106

          So how should declare it to download large size file??

          M Offline
          M Offline
          molesworth
          wrote on last edited by
          #4

          Simply put, computers don't have unlimited memory. Determine a reasonable buffer size and read the file in chunks of that size, processing each one in turn. If your data has any kind of structure (either fixed size blocks, or flexible e.g. XML) you should make use of that to work out how to read and process it in smaller pieces. I can't tell from your sample code what you're trying to do, but it appears you're accessing both local and remote files, either of which could be much larger than your available memory. You need to think through what processing you need to do, and how to achieve it within the limits.

          Days spent at sea are not deducted from one's alloted span - Phoenician proverb

          1 Reply Last reply
          0
          • M Member 12016106

            I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:

            string Files = ListBox1.SelectedValue;
            byte[] fileBytes = System.IO.File.ReadAllBytes(Files);

                    WebClient User = new WebClient();
                    byte\[\] FileBuffer = User.DownloadData(Files);
            
                    if (FileBuffer != null)
                    {
                        System.Web.HttpContext context = System.Web.HttpContext.Current;
                        context.Response.Clear();
                        context.Response.ClearHeaders();
                        context.Response.ClearContent();
                        context.Response.ContentType = "application/octet-stream";
                        context.Response.AddHeader("content-length", FileBuffer.Length.ToString());
                        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files));
                        context.Response.BinaryWrite(FileBuffer);
                        context.ApplicationInstance.CompleteRequest();
            

            Could pls anyone help...

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            You have several options:

            • Use the Response.TransmitFile method[^];
            • Use the Response.WriteFile method[^];
            • Read the file one block at a time and write each block to the response before reading the next block;

            TransmitFile is the preferred method:

            string physicalPath = ListBox1.SelectedValue;
            if (!string.IsNullOrWhiteSpace(physicalPath))
            {
            FileInfo fileToSend = new FileInfo(fileToSend);
            if (fileToSend.Exists)
            {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            context.Response.Clear();
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.ContentType = "application/octet-stream";
            context.Response.AddHeader("content-length", fileToSend.Length.ToString());
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlPathEncode(fileToSend.Name));
            context.Response.TransmitFile(fileToSend.FullName);
            context.Response.Flush();
            context.ApplicationInstance.CompleteRequest();
            }
            }

            WriteFile will work in the same way, but can have problems with large files[^]. Reading the file in small blocks requires more code, and won't perform as well as TransmitFile:

            // (Same code as before, up to and including the "Content-Disposition" header)

            int bytesRead;
            byte[] buffer = new byte[10000];
            using (Stream fileStream = fileToSend.OpenRead())
            {
            while (context.Response.IsClientConnected && (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
            context.Response.OutputStream.Write(buffer, 0, bytesRead);
            context.Response.Flush();
            }
            }

            context.ApplicationInstance.CompleteRequest();


            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups