Click here to Skip to main content
15,890,438 members
Articles / Web Development / HTML

ASP.NET File Upload with Progress Bar

Rate me:
Please Sign up or sign in to vote.
4.90/5 (252 votes)
20 Jul 2012CPOL2 min read 4.9M   68.3K   442   357
File upload widget that will display real time file upload progressbar

Introduction 

File upload widget that will display real time file upload progress bar:

FileUploadWithProgrss/Messages.png

Background

This will allow user to upload, download and delete file with classic user interface and without reloading page.

Using the Code

The upload widget includes the following:

  • File Upload interface (Default.aspx)
    • IFRAME (which contains upload engine page UploadEngine.aspx)
    • Upload button (that will allow user to upload file on server)
    • Display statistics for uploading file (Filename, Status, Progress and Transferred Bytes)
    • Grid (that will display list of uploaded files)
    • Hidden field (it will monitor and refresh file list whenever file was successfully uploaded on server)
  • Upload Engine (UploadEngine.aspx)
    • File Upload control

Page Structure (Default.aspx)

FileUploadWithProgrss/PageStructure.png

Upload Engine structure (UploadEngine.aspx)

FileUploadWithProgrss/UploadEngineStructure_.png

Page execution flow

During page loading, it will automatically register click event for Upload button.

FileUploadWithProgrss/PageExecutionFlow_1.png
  1. Choose File to upload
  2. Clicking on Upload button (it will automatically register click event for Upload button)
    • The following function is checking basic validation on client site like
      • File Upload should not be empty
      • File name should be unique
    • Executing (UploadEngine) submit event using JavaScript
    • During execution of (UploadEngine) two processes are executing simultaneously
      • Page will maintain File Upload details in session with the help of UploadDetail class
      • Every 500 milliseconds, the PageMethod will call and display progress of current uploading file with Size and Percentage value
    • Display continuous progress using StartProgress function
  3. FileUploadWithProgrss/PageExecutionFlow_2.png

  4. Display real time progress

    FileUploadWithProgrss/PageExecutionFlow_3.png

  5. Update message based on result, result may from following:
    Success Information Warning Error

    FileUploadWithProgrss/PageExecutionFlow_4.png

  6. The two processes are executing simultaneously on server side are as follows:
    • Upload Engine

      FileUploadWithProgrss/ProcessesExecution_1_.png

    • Default

      FileUploadWithProgrss/ProcessesExecution_2.png

  7. During file upload processes, the following screen would be appear:

    FileUploadWithProgrss/Downloading.png

  8. Following functionality is also available in Grid:

    FileUploadWithProgrss/Delete_DownloadFile.png

  9. I provided classical look and feel (with CSS) and cross browser compatible script (with JavaScript).
  10. The upload widgets are supported by the following browser:

    FileUploadWithProgrss/Browser.jpg

    UPDATED - 2: 

    I have added three new files for saving file in database. 

    • App_Code  
      1. SaveFile.cs  (Logic for saving file database, Attention: modify only database connection)
      2. Result.cs (this will return status success of failed with description)
    • App_Data 
      1. Script.Sql (Database script for creating Table and Stored Procedure) 

    Modified only one file UploadEngine.aspx.cs

    Please add following code after

    C#
    if (this.fileUpload.PostedFile != null && this.fileUpload.PostedFile.ContentLength > 0
    C#
    //START : Saving File in Database
                   SaveFile saveFileInDB = new SaveFile();
                   saveFileInDB.FileName = this.fileUpload.PostedFile.FileName;
                   saveFileInDB.FileExtension = Path.GetExtension(this.fileUpload.PostedFile.FileName);
                   saveFileInDB.FileContent = this.fileUpload.FileBytes;
                   Result Result = saveFileInDB.SaveFileInDB();
                   if (Result.IsError == false)
                   {
                       //File Save in Database Successfully!
                   }
                   else
                   {
                       //Error in Saving File in Database!
                       //Error: Result.ErrorMessage
                       //InnerException: Result.InnerException
                       //StackTrace: Result.StackTrace
                   }
                   //END : Saving File in Database
    

Points of Interest 

Now I will try to support multiple files upload feature.  

History  

If you find some issues or bugs with it, just leave a comment or drop me an email. If you make any notes on this, let me know that too so I don't have to redo any of your hard work.

1.                      Initial Upload

2.  21-07-12       Add functionality to uploaded file in Database also

Please provide a "Vote", if this would be helpful.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


AWARDS:

  1. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
QuestionExcellent Pin
VenAliens23-Nov-11 7:24
VenAliens23-Nov-11 7:24 
AnswerRe: Excellent Pin
Sunasara Imdadhusen23-Feb-12 23:16
professionalSunasara Imdadhusen23-Feb-12 23:16 
BugNot working properly, When i upload file more than 3 MB file Pin
rdhiman1123-Nov-11 7:19
rdhiman1123-Nov-11 7:19 
AnswerRe: Not working properly, When i upload file more than 3 MB file Pin
aryeh.tuchfeld6-Dec-11 23:00
aryeh.tuchfeld6-Dec-11 23:00 
GeneralRe: Not working properly, When i upload file more than 3 MB file Pin
shahnaitik27-Dec-11 21:12
shahnaitik27-Dec-11 21:12 
Questionupload image button Pin
Member 35753907-Nov-11 20:25
Member 35753907-Nov-11 20:25 
AnswerRe: upload image button Pin
Sunasara Imdadhusen23-Feb-12 23:19
professionalSunasara Imdadhusen23-Feb-12 23:19 
BugBUG Pin
edmonddantes229-Oct-11 17:36
edmonddantes229-Oct-11 17:36 
I believe this to be a bug:
in the javascript, you have:
C#
updateMessage(MessageStatus.Information, result.message, result.fileName, result.downloadBytes);
               if (result == 100) {
                   //clear the interval
                   window.clearInterval(intervalID);
                   clearTimeout(subintervalID);
               }

You do see that result is an object, hence we NEVER have result==100, nor
is it ever coerced to be 100.
You can put an alert box in there and you see it never executes.
Questionsome Files missing Pin
marko_shock@yahoo.com29-Oct-11 2:03
marko_shock@yahoo.com29-Oct-11 2:03 
Questionissue when deploying application localhost Pin
Lutchmee28-Oct-11 13:55
Lutchmee28-Oct-11 13:55 
Questionhave a problem while Writing the byte to disk Pin
y_srikanth11-Oct-11 3:53
y_srikanth11-Oct-11 3:53 
AnswerRe: have a problem while Writing the byte to disk Pin
CDP180211-Oct-11 4:17
CDP180211-Oct-11 4:17 
QuestionFireFox Pin
sarkpaul29-Sep-11 5:03
sarkpaul29-Sep-11 5:03 
AnswerRe: FireFox Pin
Fréderick VdH2-Oct-11 20:53
Fréderick VdH2-Oct-11 20:53 
AnswerRe: FireFox Pin
brother.gabriel29-Apr-12 16:10
brother.gabriel29-Apr-12 16:10 
QuestionInstallation Pin
kazzoul19-Sep-11 1:46
kazzoul19-Sep-11 1:46 
AnswerRe: Installation Pin
Sunasara Imdadhusen20-Sep-11 22:11
professionalSunasara Imdadhusen20-Sep-11 22:11 
GeneralWhere can i set timer for progress while uploading Pin
demouser74318-Sep-11 21:58
demouser74318-Sep-11 21:58 
AnswerRe: Where can i set timer for progress while uploading Pin
Sunasara Imdadhusen20-Sep-11 22:08
professionalSunasara Imdadhusen20-Sep-11 22:08 
GeneralRe: Where can i set timer for progress while uploading Pin
demouser74320-Sep-11 23:03
demouser74320-Sep-11 23:03 
QuestionError: CS0246: The type or namespace name 'UploadDetail' could not be found (are you missing a using directive or an assembly reference?) Pin
Member 300518415-Sep-11 6:53
Member 300518415-Sep-11 6:53 
GeneralMy vote of 5 Pin
cjhlyfe12-Sep-11 18:57
cjhlyfe12-Sep-11 18:57 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen13-Sep-11 3:10
professionalSunasara Imdadhusen13-Sep-11 3:10 
GeneralRe: My vote of 5 Pin
cjhlyfe26-Sep-11 10:45
cjhlyfe26-Sep-11 10:45 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen26-Sep-11 21:51
professionalSunasara Imdadhusen26-Sep-11 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.