.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Showing progress with FileUpload Control

Posted By:Gaurav Arora       Posted Date: August 22, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Progress Bar for FileUploadControl - Asp.net


Many times I have asked this question that How can I show a progress Bar with FileUloadControl in Asp.net?

Its a very easy and a tactic task. The logic behind this is just to get the total time take for upload a file into server. The following code-snippet describes the same:


Design File:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileUpload.aspx.cs" Inherits="fileUpload" %>

    
    File Upload

    

    
    
        

            File Upload Example
        
            CssClass="lbl" />
        
        
        
        
            OnClick="btnAddImage_Click" />
        
        
            
                Uploading...
            
                
                    display: none">
                
            
            
                
            
            
        
    
    
    
        
            
                
                    runat="server" AlternateText='<%# Eval("Name") %>' />
                
                <%# Eval("Name") %>
            
        
    
    

Code-Behind File:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class fileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAddImage_Click(object sender, EventArgs e)
    {
        if (ImageFile.HasFile)  //if file uploaded
        {
            if (checkFileType(ImageFile.FileName))  //Check for file types
            {
                try
                {
                    //save file
                    ImageFile.SaveAs(MapPath("~/UploadImages/" + ImageFile.FileName));
                    //Response.Write("");
                    System.Threading.Thread.Sleep(8000);
                    Label1.Text = "Upload successfull!";
                }
                catch (System.IO.DirectoryNotFoundException)
                {
                    createDir();
                }
            }
        }
        else
        {
            Response.Write("");
        }
    }
    private bool checkFileType(string fileName)
    {
        string fileExt = Path.GetExtension(ImageFile.FileName);

        switch (fileExt.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
        }
        
    }
    private void createDir()
    {
        DirectoryInfo myDir = new DirectoryInfo(MapPath("~/UploadImages/"));
        myDir.Create();

        //Now save file
        ImageFile.SaveAs(MapPath("~/UploadImages/" + ImageFile.FileName));
        Response.Write("");
    }
    protected void btnShowImage_Click(object sender, EventArgs e)
    {
        DirectoryInfo myDir = new DirectoryInfo(MapPath("~/UploadImages/"));
        try
        {

            dlImageList.DataSource = myDir.GetFiles();
            dlImageList.DataBind();

        }
        catch (System.IO.DirectoryNotFoundException)
        {
            Response.Write("");
        }
    }
    
}

Try above code and you will get following output as shown in snapshots:
  • Initial stage of the running code/application


    • Select to upload an image file [I have developed a sample which excepts only Image files]

    • Uploading images with Progress in %age

    • Message, when upload completed
    • Clicking on 'ShowImage' button will display uploaded image(s)

    Note:
  • In above code, the actual time of uploading may be different
  • Progress in on the basis of 100-chunks
  • Please feel free to revert back for any further assistant.




  •      

    Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More code samples in C#, ASP.Net, Vb.Net and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend