File Upload and Download using Asp.net C#
In this article we will discuss about How to upload and download files in a particular folder in ASP.net with the help of C sharp.
Here is my .aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploadcheck.aspx.cs" Inherits="fileuploadcheck" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="uploadtable">
<tr>
<td><asp:FileUpload class="textbox" ID="Fu1" runat="server" /></td>
</tr>
<tr>
<td><asp:Label class="label1" ID="Label1" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<asp:Button class="submit" ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Here is C# Code Behind the upload button:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (Fu1.HasFile)
{
FileInfo fi = new FileInfo(Fu1.FileName);
if (fi.Extension != ".docx" && fi.Extension != ".pdf" && fi.Extension != ".doc")
{
Label1.Text = "Unsupported File Format";
}
else
{
int fileSize = Fu1.PostedFile.ContentLength;
if (fileSize > (2 * 1024 * 1024))
{
Label1.Text = "Too big";
}
else
{
Fu1.SaveAs(Server.MapPath("~/upload/" + Fu1.FileName));
Label1.Text = "File Uploaded Successfully";
}
}
}
else
{
Label1.Text = "Please Select file";
}
}
}
Here the output:
Here is my .aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploadcheck.aspx.cs" Inherits="fileuploadcheck" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="uploadtable">
<tr>
<td><asp:FileUpload class="textbox" ID="Fu1" runat="server" /></td>
</tr>
<tr>
<td><asp:Label class="label1" ID="Label1" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<asp:Button class="submit" ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Here is C# Code Behind the upload button:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (Fu1.HasFile)
{
FileInfo fi = new FileInfo(Fu1.FileName);
if (fi.Extension != ".docx" && fi.Extension != ".pdf" && fi.Extension != ".doc")
{
Label1.Text = "Unsupported File Format";
}
else
{
int fileSize = Fu1.PostedFile.ContentLength;
if (fileSize > (2 * 1024 * 1024))
{
Label1.Text = "Too big";
}
else
{
Fu1.SaveAs(Server.MapPath("~/upload/" + Fu1.FileName));
Label1.Text = "File Uploaded Successfully";
}
}
}
else
{
Label1.Text = "Please Select file";
}
}
}
Here the output:
No comments:
Post a Comment