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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
CPSRIAuthor Commented:
hi Tchuki ,

 Is it work in Framework 2.0.? i tried it in Framework 2.0 but not working ..
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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
Vel EousResearch & Development ManagerCommented:
Perhaps this tutorial will provide a better explanation for you:

http://asp.net-tutorials.com/controls/file-upload-control/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CPSRIAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.