File Upload control w.r.t. size of File in C# asp.net

Hi ,
   i am using three File Upload controls  in my project . My requirement is allow user to upload only 1 MB Maximum File size from Each Control . If the user upload file size more than 1 MB display An Error Message to the User .How can i handle this at code behind in C# .Net . Thanks in Advance.
CPSRIAsked:
Who is Participating?
 
Vel EousConnect With a Mentor Research & Development ManagerCommented:
Perhaps this tutorial will provide a better explanation for you:

http://asp.net-tutorials.com/controls/file-upload-control/
0
 
Vel EousResearch & Development ManagerCommented:
You can use the FileInfo class to determine the filesize prior to processing it, for example:

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    if ((openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) && !string.IsNullOrEmpty(openFileDialog.FileName))
    {
        bool fileOver1MB = false;
        FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
        float fileSize = (fileInfo.Length / 1024f) / 1024f;
        if (fileSize > 1)
        {
            fileOver1MB = true;
        }
        else
        {
            fileOver1MB = false;
        }
    }
}

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
CPSRIAuthor Commented:
hi Tchuki ,

 Is it work in Framework 2.0.? i tried it in Framework 2.0 but not working ..
0
 
CPSRIAuthor Commented:
hi Tchuki .. while executing your give code raising the bellow exception ..
"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."
0
 
CPSRIAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.