FileUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="btnUploadFile" runat="server" Text="Upload File" OnClick="btnUploadFile_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This is a required field!"
ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
</div>
</form>
</body>
</html>
FileUpload.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUploadFile_Click(object sender, EventArgs e)
{
//Simple upload without restriction:
//if (FileUpload1.HasFile)
//{
// try
// {
// string filePath = Server.MapPath("~/FileUpload/" + FileUpload1.FileName);
// FileUpload1.SaveAs(filePath);
// //FileUpload1.SaveAs("~/FileUpload/" +
// // FileUpload1.FileName);
// Label1.Text = "File Name" +
// FileUpload1.PostedFile.FileName + "<br />" +
// FileUpload1.PostedFile.ContentLength + "kb <br />" +
// "Content Type: " +
// FileUpload1.PostedFile.ContentType;
// }
// catch (Exception ex)
// {
// Label1.Text = "Error" + ex.Message.ToString();
// }
//}
//else
//{
// Label1.Text = "You have not Specified a File.";
//}
//File upload with Restriction:
if (FileUpload1.HasFile)
{
int maxFileSize = 1048576; //1GB
int fileSize = FileUpload1.PostedFile.ContentLength / 1024; // For FileSize in MB: FileUpload1.PostedFile.ContentLength / (1024 * 1024)
if (fileSize > maxFileSize)
{
Label1.Text = "File size exceeded the maximum limit of " + maxFileSize / 1024 + " Kb.";
}
else
{
string fileExt =
System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt == ".mp3")
{
try
{
string filePath = Server.MapPath("~/FileUpload/" + FileUpload1.FileName);
FileUpload1.SaveAs(filePath);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "Only .mp3 files allowed!";
}
}
}
else
{
Label1.Text = "You have not specified a file.";
}
}
}
//changes in web.config file;
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime
executionTimeout="1100"
maxRequestLength="1048576"
requestLengthDiskThreshold="80"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="5000"
enableKernelOutputCache="true"
enableVersionHeader="true"
requireRootedSaveAsPath="true"
enable="true"
shutdownTimeout="90"
delayNotificationTimeout="5"
waitChangeNotification="0"
maxWaitChangeNotification="0"
enableHeaderChecking="true"
sendCacheControlHeader="true"
apartmentThreading="false" />
</system.web>
</configuration>
Note: Defination for know term:
maxRequestLength - Attribute limits the file upload size for ASP.NET application. This limit can be used to prevent denial of service attacks (DOS) caused by users posting large files to the server. The size specified is in kilobytes. As mentioned earlier, the default is "4096" (4 MB). Max value is "1048576" (1 GB) for .NET Framework 1.0/1.1 and "2097151" (2 GB) for .NET Framework 2.0.
executionTimeout - Attribute indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by the application. The executionTimeout value should always be longer than the amount of time that the upload process can take.
No comments:
Post a Comment