File upload. Huge files

Discussion in 'ASP .Net Building Controls' started by Oleg Slyusarchuk, Jul 29, 2004.

  1. Hi,
    This is topic that has been discussed thousand times, however.
    Classic approach is to use HttpPostedFile object, check if it's not null,
    and save it.

    Something like:

    // Check to see if file was uploaded
    if( filMyFile.PostedFile != null ){
    // Get a reference to PostedFile object
    HttpPostedFile myFile = filMyFile.PostedFile;
    // Get size of uploaded file
    int nFileLen = myFile.ContentLength;
    // make sure the size of the file is > 0
    if( nFileLen > 0 ){
    // Allocate a buffer for reading of the file
    byte[] myData = new byte[nFileLen];
    // Read uploaded file from the Stream
    myFile.InputStream.Read(myData, 0, nFileLen);
    // Create a name for the file to store
    string strFilename = Path.GetFileName(myFile.FileName);
    // Write data into a file
    WriteToFile(Server.MapPath(strFilename), ref myData);

    or even easier:

    if (File1.PostedFile !=null)
    //Checking for valid file { // PostedFile.FileName gives the entire
    path.Use Substring function to rip of the filename.
    string StrFileName =
    \") + 1) ;
    string StrFileType = File1.PostedFile.ContentType ;
    int IntFileSize =File1.PostedFile.ContentLength; //Checking for the file
    if (IntFileSize <=0) Response.Write(" Uploading of file " + StrFileName + "
    failed ");
    else { File1.PostedFile.SaveAs(Server.MapPath(".\\" + StrFileName));
    Response.Write( "Your file " + StrFileName + " of type " + StrFileType + "
    and size " + IntFileSize.ToString() + " was uploaded successfully"); } }

    Everything works fine for relatively small files (say, a couple megs).

    Hoverver, if files becomes big (say, 100Mb), and a couple files are being
    uploded at the same time,
    required memory grows, and finelly system may crash.


    Is there any way to read file in parts (similar to DB-based ReadChunk)
    What an appropriate aproach can be used in this situation?
    May I get a reference to response stream before all the file is uploaded?


    Oleg Slyusarchuk, Jul 29, 2004
    1. Advertisements

  2. Oleg Slyusarchuk

    Matt Sollars Guest

    Hi, Oleg:

    To my knowledge, there is no easy solution to this problem. You will
    have to write a custom HttpHandler and perform the I/O manually in chunks.

    However, some folks have already done the work for you. I recommend one
    of the following:

    - ABCUpload .NET by Web Super Goo
    - FileUp by SoftArtisans (

    I can vouche for the first one on a production server with large
    uploads. It runs great. It isn't the cheapest component, but it's under
    a grand and saves a lot of headache and custom code that would've cost
    me more in time than the cost. I played with a demo of the second one
    and it seemed competent enough to handle large files as well. They both
    offer a pure HTML progress indicator solution as well.

    I hope this helps,

    Matt Sollars, Aug 4, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe
    Lau Lei Cheong
    Jan 6, 2005
  2. Jeff

    upload/download huge files

    Jeff, Mar 10, 2006, in forum: ASP .Net
    John Timney \( MVP \)
    Mar 16, 2006
  3. Replies:
    Randy Webb
    Jan 8, 2005
  4. Replies:
    Randy Webb
    Jan 8, 2005
  5. Replies:

Share This Page