Hi guys,
This blog we discuss how to write asp.net code
for file upload and file download .
Fileupload.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style4
{
width: 100%;
}
.auto-style5
{
width: 244px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style4">
<tr>
<td class="auto-style5">FileName</td>
<td>
<asp:TextBox ID="txtfilename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5">FileUpload</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style5"> </td>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
<asp:Label ID="lblMsg" runat="server" Text="Msg"></asp:Label>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="FilePath">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FileName" HeaderText="FileName" />
<asp:BoundField DataField="FilePath" HeaderText="FilePath" />
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton ID="mylink" CommandName="linkit" Text="Download" runat="server" OnClick="my_download" CommandArgument='<%Eval("FilePath")
%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Fileupload.aspx.cs
file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;
using System.IO;
using System.Drawing;
using System.Net;
public partial class imagestore : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["111"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindme();
}
}
protected void bindme()
{
SqlDataAdapter sdr = new SqlDataAdapter("select * from
myimage1", con);
DataSet ds = new DataSet();
sdr.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("Images/" +
filename));
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("Insert into myimage1(FileName,FilePath)values(@FileName,@FilePath)", con);
cmd.Parameters.AddWithValue("@FileName",
txtfilename.Text);
cmd.Parameters.AddWithValue("@FilePath", "Images/" +
filename);
cmd.ExecuteNonQuery();
con.Close();
lblMsg.ForeColor = Color.Green;
lblMsg.Text = "Successfully Uploaded file data";
}
catch (Exception er)
{
lblMsg.Text = er.Message;
}
bindme();
}
protected void my_download(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filepath =
GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filepath + "\"");
Response.TransmitFile(Server.MapPath(filepath));
Response.End();
//------------------------------------------
}
}
No comments:
Post a Comment