We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Tuesday, July 21, 2015

File Upload and Download Example

Design.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Save and Download Files </title>
<style type="text/css">
#h1 {
text-align:center;
align-content:center;
width:500px;
font-family:Kremlin Georgian;
background-color:#3d0b0b;
border:5px solid darkblue;
-webkit-animationmymove 5s infinite;
animationmymove 5s infinite;
outline1px solid black;


border-radius:10px;

}


#grdview1 {


-webkit-animationmymove 5s infinite;
animationmymove 5s infinite;
outline2px solid black;
border-radius:20px;
border:5px solid darkblue;
  
}
     
@-webkit-keyframes mymove {


50% {outline15px solid lightblue;}
}
/* Standard syntax */


@keyframes mymove {
50% {outline15px solid lightblue;}



}
</style>


</head>
<body>
<form id="form1" runat="server">
<div align="center"><h1 id="h1" align="center" style="color:green">File Upload and Download</h1></div>
<div align="center"><div align="center" style="border:groove;align-items:centerwidth:500px;border-radius:15px 15px 15px 15px;box-shadow:10px 10px 10px 10px darkgreen;">
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<br /><br />
<asp:FileUpload ID="fileUpload1" runat="server"/><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</div></div>


<br /><br />
<div align="center">
<asp:GridView ID="grdview1" BorderColor="Green" runat="server" AutoGenerateColumns="False" DataKeyNames="FilePath" BackColor="#DEBA84"BorderStyle="Dashed" BorderWidth="3px" CellPadding="3" CellSpacing="2">
<Columns>
<asp:BoundField DataField="Id" HeaderText="FileId" />
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:TemplateField HeaderText="File Path">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


  </Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
</div>
</form>
</body>
</html>

Design.aspx.cs


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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page

{SqlConnection con = null;

SqlCommand cmd = null;

SqlDataAdapter da = null;

DataSet ds = null;

string strSqlCommand = string.


 

protected void Page_Load(object senderEventArgs e)

{con = new SqlConnection(@"Data Source=mithilesh;database=SystemTest;User Id=sa;Password=12345;");

if (!Page.IsPostBack)

{BindData();

}

}
   private void BindData

{
 
strSqlCommand = "select * from FileUpDown";

da = new SqlDataAdapter(strSqlCommandcon);

ds = new DataSet();

da.Fill(ds"FileUpDown");

grdview1.DataSource = ds;

grdview1.DataBind();

} 
protected void lnkDownload_Click(object senderEventArgs e)

{   LinkButton lnkbtn = sender as LinkButton;

GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;

string filePath = grdview1.DataKeys[gvrow.RowIndex].Value.ToString();

//Response.ContentType = "image/jpg";

Response.AddHeader("Content-Disposition""attachement;filename=\"" + filePath + "\"");

Response.TransmitFile(Server.MapPath(filePath));

Response.End();


}
   protected void btnUpload_Click(object senderEventArgs e)

{ string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);

if(fileName!=null

{
  
 fileUpload1.SaveAs(Server.MapPath("Files/" + fileName));

con.Open();
  strSqlCommand = "Insert into FileUpDown(FileName,FilePath)values(@FileName,@FilePath)";

cmd = new SqlCommand(strSqlCommandcon);

cmd.Parameters.AddWithValue("@FileName"fileName);

cmd.Parameters.AddWithValue("@FilePath""Files/" + fileName);

if (cmd.ExecuteNonQuery() > 0)

{   lblStatus.Text = "<b style='color:green'>File Uploaded Successfully</b>";

}
}
  
 if (fileName == null)
{
  
 lblStatus.Text = "<b style='color:red'>Please select the any content</b>";
}
  
 con.Close();
BindData();
}


 For Upload file click choose the file and click the upload




 For download, the document click download option




No comments: