views:

813

answers:

6

Whats the best way to check the size of a file during upload using asp.net and C#? I can upload large files by altering my web.config without any problems. My issues arises when a file is uploaded which is more than my allowed max file size.

I have looked into using activex objects but that is not cross browser compatible and not the best answer to the solution. I need it to be cross browser compatible if possible and to support IE6 (i know what you are thinking!! However 80% of my apps users are IE6 and this is not going to change anytime soon unfortunately).

Has any dev out there come across the same problem? And if so how did you solve it?

Many thanks

Cragly

A: 

This could be done with Silverlight or Flash. For Falsh you can see swfupload.

Mike Chaliy
swfupload is free and good. Used it many times and it really answers the question. +1 here.
Nir Levy
Mike, why did you rollback the revision I made?
Andreas Grech
Dreas, why did you made that revision? Did you added information?
Mike Chaliy
I ended up using Telerik Upload but did do what I asked
Cragly
A: 

We are currently using NeatUpload to upload the files.

While this does the size check post upload and so may not meet your requirements, and while it has the option to use SWFUPLOAD to upload the files and check size etc, it is possible to set the options such that it doesn't use this component.

Due to the way they post back to a postback handler it is also possible to show a progress bar of the upload. You can also reject the upload early in the handler if the size of the file, using the content size property, exceeds the size you require.

David McEwing
Aha, another Flash based solution.
Mike Chaliy
Actually no. Depending on the options you choose, there is no flash involved, which is why we choose it!
David McEwing
There is no way other then flash or something simmilar to check size of the file. Also I have inspected theirs demo page, they are just wrapper on swfupload and yes this is Flash.
Mike Chaliy
+4  A: 

If you are using System.Web.UI.WebControls.FileUpload control:

MyFileUploadControl.PostedFile.ContentLength;

Returns the size of the posted file, in bytes.

Andreas Grech
During upload, or after?
Robert Harvey
During a Postback...the file is saved only when you call MyFileUploadControl.PostedFile.SaveAs("file.txt");
Andreas Grech
Except the postback is only triggered once the upload has finished. So it's actually *after* not during.
blowdart
You cannot check the size of the file before the file is posted to the server because client-side technologies (javascript) cannot access local files because of security measures.
Andreas Grech
+1  A: 

This is what I do when uploading a file, it might help you? I do a check on filesize among other things.

//did the user upload any file?
            if (FileUpload1.HasFile)
            {
                //Get the name of the file
                string fileName = FileUpload1.FileName;

            //Does the file already exist?
            if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["fileUploadPath"].ToString() + fileName)))
            {
                PanelError.Visible = true;
                lblError.Text = "A file with the name <b>" + fileName + "</b> already exists on the server.";
                return;
            }

            //Is the file too big to upload?
            int fileSize = FileUpload1.PostedFile.ContentLength;
            if (fileSize > (maxFileSize * 1024))
            {
                PanelError.Visible = true;
                lblError.Text = "Filesize of image is too large. Maximum file size permitted is " + maxFileSize + "KB";
                return;
            }

            //check that the file is of the permitted file type
            string fileExtension = Path.GetExtension(fileName);

            fileExtension = fileExtension.ToLower();

            string[] acceptedFileTypes = new string[7];
            acceptedFileTypes[0] = ".pdf";
            acceptedFileTypes[1] = ".doc";
            acceptedFileTypes[2] = ".docx";
            acceptedFileTypes[3] = ".jpg";
            acceptedFileTypes[4] = ".jpeg";
            acceptedFileTypes[5] = ".gif";
            acceptedFileTypes[6] = ".png";

            bool acceptFile = false;

            //should we accept the file?
            for (int i = 0; i <= 6; i++)
            {
                if (fileExtension == acceptedFileTypes[i])
                {
                    //accept the file, yay!
                    acceptFile = true;
                }
            }

            if (!acceptFile)
            {
                PanelError.Visible = true;
                lblError.Text = "The file you are trying to upload is not a permitted file type!";
                return;
            }

            //upload the file onto the server
            FileUpload1.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["fileUploadPath"].ToString() + fileName));
        }`
macou
of course this server side Mark Graham. The original question asked "Whats the best way to check the size of a file during upload using asp.net and C#?"
macou
A: 

Last post - you are missing the point; this is done SERVER side!!

Original post wants to know if file size can be check prior to uploading a large file, presumably to reduce bandwidth usage (which is my issue incidentally), and server processing saving large files, because users don't know how to reduce the size of an image.

Here's a thread

I'm looking into this myself. If I figure it out I will post something here.

A: 

You can do it on Safari and FF simply by

<input name='file' type='file'>    

alert(file_field.files[0].fileSize)
balepc