<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>
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); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)