Step 1: First since file upload control cannot upload a large file (say 40MB) by default we need to set the size in the web.config file
<system.web>
<httpRuntime executionTimeout="9876" maxRequestLength="41943040"/>
</system.web>
Step 2:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// You can specify your file type here
if (fileupload.PostedFile.ContentType == "application/pdf")
{
if (fileupload.HasFile)
{
//41943040 - 40mb
if (fileupload.PostedFile.ContentLength > 41943040)
{
lblMessage.Text = "File size exceeds maximum limit of 40 MB.";
}
else
{
//Here you can have your own logic to save the uploaded file to disk or database as required
long fileSize = fileupload.PostedFile.ContentLength;
bool result = Upload(fileupload.PostedFile, fileSize);
if (result)
lblMessage.Text = "File with size " + (fileSize / 1024).ToString() + " kb uploaded successfully";
else
lblMessage.Text = "Error uploading file.";
}
}
}
else
{
lblMessage.Text = "File type should be pdf.";
}
}
}