Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i need to check the file size of my file being uploaded I"m doing the following things

Fileupload.aspx:
C#
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/Fileupload.js"></script>
</head>
<body>

    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="Enter your name:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:FileUpload ID="fUpload" runat="server" />
         <asp:Button runat="server" id="btnGetSize" text="Upload" onclick="btnGetSize_Click" />
          <asp:CustomValidator ID="CustomValidator1" runat="server"  
                    Text="*" ToolTip="FileSize should not exceed 5kb" 
                    ErrorMessage="FileSize Exceeds the Limits.Please Try uploading smaller size files." 
                    ControlToValidate="fUpload"  
                     ClientValidationFunction ="GetFileSize('fUpload');"/>

         <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
    </div>
    </form>
</body>
</html>


On Fileuploadaspx.cs:
C#
  protected void btnGetSize_Click(object sender, EventArgs e)
        {
            if (fUpload.HasFile ) {
                fUpload.SaveAs(@"C:\temp\" + fUpload.FileName);
                StatusLabel.Text = "File Uploaded: " + fUpload.FileName;
            }
                
        }
</pree>
on Fileupload.js
<pre lang="Javascript">
function GetFileSize(fileid) {
try {
var fileSize = 0;
//for IE
if ($.browser.msie) {
//before making an object of ActiveXObject,
//please make sure ActiveX is enabled in your IE browser
var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
fileSize = fileSize / 1048576; //size in mb
}
//for FF, Safari, Opeara and Others
else {
fileSize = $("#" + fileid)[0].files[0].size //size in kb
fileSize = fileSize / 1048576; //size in mb
}
alert("Uploaded File Size is" + fileSize + "MB");
}
catch (e) {
alert("Error is :" + e);
}
}


but it is not working..Please tell me where i am going wrong
Posted

1 solution

I don't think that there would be any way to get the size of a file that is residing on client side using javascript. I mean it makes no sense to give the coder the freedom to view details of any file on clients machine. However you may check this out http://stackoverflow.com/questions/1601455/check-file-input-size-with-jquery as part of HTML5

Regards
Pawan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900