Link to home
Start Free TrialLog in
Avatar of CPSRI
CPSRIFlag for United States of America

asked on

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.
Avatar of Vel Eous
Vel Eous

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

Avatar of Éric Moreau
Avatar of CPSRI

ASKER

hi Tchuki ,

 Is it work in Framework 2.0.? i tried it in Framework 2.0 but not working ..
Avatar of CPSRI

ASKER

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."
ASKER CERTIFIED SOLUTION
Avatar of Vel Eous
Vel Eous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CPSRI

ASKER

Thanks