Click here to Skip to main content
15,884,629 members
Articles / Web Development / CSS

Professional file uploads with the MultiUpload and ImageList server controls

Rate me:
Please Sign up or sign in to vote.
1.80/5 (2 votes)
19 Dec 2007GPL35 min read 51.6K   480   68  
A professional AJAX GUI for uploading files using the server controls in the Memba Velodoc XP Edition.
using System;
using System.Data;
using System.Configuration;
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; //Directory, Path, FileInfo

public partial class _Default : System.Web.UI.Page 
{
    private const string TARGET_DIR = "~/";

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        //Check target directory
        string sTargetDir = Request.MapPath(TARGET_DIR);
        System.Diagnostics.Debug.Assert(Directory.Exists(sTargetDir));

        //Iterate through posted files (foreach does not work with empty file inputs)
        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFile objFile = Request.Files[i];
            //Make sure file input has content
            if ((objFile.ContentLength > 0) && (objFile.FileName.Length > 0))
            {
                //Get target file path for save as
                string sFileName = Path.GetFileName(objFile.FileName);
                string sFilePath = Path.Combine(sTargetDir, sFileName);
                FileInfo objFileInfo = new FileInfo(sFilePath);
                //No business rule, i.e. we just want to avoid failure
                if (objFileInfo.Exists)
                {
                    objFileInfo.Attributes &= ~FileAttributes.ReadOnly;
                    objFileInfo.Delete();
                }
                //Save file
                objFile.SaveAs(sFilePath);
            }
        }

        //Clear the list, otherwise the ImageList has items
        //Which the MuliUpload component does not have
        ImageList.ImageListItemCollection.Clear();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Other Memba
United Kingdom United Kingdom

Other Articles:



My work at Memba


You can check what Memba does at our corporate web site.

I am currently working on two projects:
- VELODOC, a file transfer online service and software product which you can try at http://www.velodoc.net;
- CITADOC, a knowledge base/wiki online service and software product which can be managed and edited in Microsoft Office and which will be released in the second half of 2008.

The XP edition of these projects is the open source core released under the GPL license.

Comments and Discussions