Usage of FileUpload web server Control

Written by omerkamal on Mar 23, 2007
Different senarioes of Fileupload control in ASP .NET 2.0

Explanation:

1. Save the File to your specified distination

    // Specify the path on the server to save the uploaded file to.
    String savePath = @"c:\root\uploads\";

    // Before attempting to perform operations/ on the file, verify that the FileUpload
    // control contains a file.
    if (FileUpload1.HasFile)
    {
      // Get the name of the file to upload.
      String fileName = FileUpload1.FileName;
     
      // Append the name of the file to upload to the path.
      savePath += fileName;

      FileUpload1.SaveAs(savePath);
     }

2. Save files to a specified directory using PhysicalApplicationPath

       // specif y the upload Directory to cater the website files overwrite like web.config.
       string saveDir = @"\Uploads\"; 

       // Get the physical file system path for the currently executing application.
      string appPath = Request.PhysicalApplicationPath;


     if (FileUpload1.HasFile)
         {
             string savePath = appPath + saveDir +
                                             Server.HtmlEncode(FileUpload1.FileName);
            
                  ....................
         }
           
3. Check the uploaded File size

             // Get the size in bytes of the file to upload.
            int fileSize = FileUpload1.PostedFile.ContentLength;
         
            // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
            if (fileSize < 2100000)
            {
                     ...............
            }

4. Check the uploaded File Extension

            // Get the extension of the uploaded file.
            string extension = System.IO.Path.GetExtension(fileName);
           
            // Allow only files with .doc or .xls extensions to be uploaded.
            if ((extension == ".doc") | (extension == ".xls"))
            {
                .................
             }
Visitors/Readers Comments
(for questions please use The Forum)



Sri

Pretty  neat and straightforward article. Great!!!

 

 

04/09/2007 14:31:05 UTC




Add your Comments

Name:  
Message:


Advertise Here
For more details
Contact Us

Advertisments