Get posted file's content length (file size)
FileUpload is an asp.net web server control. FileUpload control allow users to select a file to upload to the web server.
FileUpload.PostedFile property allow us to get the underlying HttpPostedFile object for a file that is uploaded by using the
FileUpload control.
FileUpload control's PostedFile property value type is System.Web.HttpPostedFile which represents an HttpPostedFile for a file uploaded by using the FileUpload control. this property allow us to access additional properties on the uploaded file.
.net developers can use the ContentLength property to get the length of the file. the ContentLength property is used to determine the length of the file before copying the file contents to a byte array. we can access ContentLength property as FileUpload.PostedFile.ContentLength.
the following asp.net c# example code demonstrate us how can we get FileUpload control's uploaded file content length (file size) programmatically in an asp.net application.
FileUpload control's PostedFile property value type is System.Web.HttpPostedFile which represents an HttpPostedFile for a file uploaded by using the FileUpload control. this property allow us to access additional properties on the uploaded file.
.net developers can use the ContentLength property to get the length of the file. the ContentLength property is used to determine the length of the file before copying the file contents to a byte array. we can access ContentLength property as FileUpload.PostedFile.ContentLength.
the following asp.net c# example code demonstrate us how can we get FileUpload control's uploaded file content length (file size) programmatically in an asp.net application.
FileUploadPostedFileContentLength.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string uploadFolder = Request.PhysicalApplicationPath + "TestUpload\\";
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
Label1.Text = "This file uploaded successfully!<br />Posted File Content Length: "
+ FileUpload1.PostedFile.ContentLength;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net FileUpload example: how to get posted file content length (size) after upload file</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net FileUpload example: Posted File Content Length</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
Font-Italic="true"
Font-Bold="true"
ForeColor="HotPink"
>
</asp:Label>
<br /><br />
<asp:FileUpload
ID="FileUpload1"
runat="server"
BackColor="DarkMagenta"
ForeColor="FloralWhite"
/>
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="DarkMagenta"
OnClick="Button1_Click"
Text="Upload Now"
/>
<br /><br />
</div>
</form>
</body>
</html>
- asp.net FileUpload example: how to upload file with specific extension
- asp.net FileUpload validation example: how to validate FileUpload control (file upload)
- asp.net FileUpload example: how to check whether FileUpload control has file
- asp.net FileUpload example: how to check (handle) error when upload a file
- asp.net FileUpload example: how to get posted file full name after upload a file
- asp.net FileUpload example: how to get posted file content type after upload file
- asp.net FileUpload example: how to rename file when upload (change file name when upload)
- FileUpload example: how to use FileUpload control in asp.net