Home > Uncategorized > Upload with Progress Bar

Upload with Progress Bar

I found an awesome control for uploading via web browser, WITH a progress bar. Dean Brettle is the author, and his website is at: http://www.brettle.com . It’s a .NET control and it’s very easy to use.

I did have a need for multiple upload controls in a single page, with the option to add more as needed. The upload control didn’t have the capability built in to handle a collection, so I created one myself with the use of the System.Collections.Generic namespace. This is the code I used for uploading multiple images:

1. Included the System.Collections.Generic namespace

2. Added this generic property to my class:

List<InputFile> _InputFileCollection = new List<InputFile>();
    public List<InputFile> InputFileCollection
    {
        get
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(InputFile))
                {
                    _InputFileCollection.Add((InputFile)ctrl);
                }
            }
            return _InputFileCollection;
        }
    }

3. On the button click/upload event:

foreach (InputFile file in InputFileCollection)
        {
            if(file.HasFile)
           {
               _CurrentImageName = file.FileName;
               if(File.Exists(SavePath + _CurrentImageName))
               {
                   int i = 0;
                   while(File.Exists(SavePath + _CurrentImageName))
                   {
                       string s = file.FileName;
                       int dotLocation = s.IndexOf(”.”);
                       s = s.Insert(dotLocation,i.ToString());
                       _CurrentImageName = s;
                       i++;
                   }
               }
               file.MoveTo(SavePath + _CurrentImageName, MoveToOptions.Overwrite);
            }
        }

Works fantastically, and you can add all the input controls you want. 

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.