Upload a file in ASP.Net
File uploading is very necessary thing to
develop a dynamic website. By default ASP.Net support file upload upto 4MB of
size. If you want to upload more then you have to configure web.config file. In next post i will explain how to
upload large file in ASP.NET.
Add the content within an ASP.Net page
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!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>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" /></div>
</form>
</body>
</html>
One file upload control and one button will
apear in page. Now write the folowing code in button’s click event. It will
upload a file into D: directory.
protected void Button1_Click(object sender, EventArgs
e)
{
if
(FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs("D:\\" + FileUpload1.FileName);
}
catch
(Exception ex)
{
Response.Write(“Problem in file
upload” + ex.Message);
}
}
}
No comments:
Post a Comment