In This article we are going to see how to create a application to upload a file and list the content as gridview and while click the name of file in gridview download the file to the user.
File Upload
Make a File upload control and save the file in server under files folder, Make an entry in gridview
Html
C#
Download
For download we have to change the response of the click event in rowcommand event of gridview.
C#:
This article will help you the guys who want to create a code for upload and download the file.
File Upload
Make a File upload control and save the file in server under files folder, Make an entry in gridview
Html
<form id="form1" runat="server">
<div align="center">
<br />
<asp:FileUpload ID="fileupload1"runat="server"/>
<asp:Button ID="upload" Text="Upload"runat="server"onclick="upload_Click"/>
<br />
<br />
<asp:GridView ID="GridView1"runat="server"AutoGenerateColumns="False"
CellPadding="4"EnableModelValidation="True"ForeColor="#333333"
GridLines="None"onrowcommand="GridView1_RowCommand">
<AlternatingRowStyleBackColor="White"ForeColor="#284775"/>
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="download"
CommandArgument='<%# Eval("Name") %>' Text='<%# Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Type"HeaderText="Type"/>
<asp:BoundField DataField="Size"HeaderText="Size in Bytes" />
</Columns>
<EditRowStyle BackColor="#999999"/>
<FooterStyle BackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<HeaderStyle BackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<PagerStyle BackColor="#284775"ForeColor="White"HorizontalAlign="Center"/>
<RowStyle BackColor="#F7F6F3"ForeColor="#333333"/>
<SelectedRowStyleBackColor="#E2DED6"Font-Bold="True"ForeColor="#333333"/>
</asp:GridView>
</div>
</form>
C#
DataTable dt = newDataTable();
protected voidPage_Load(object sender, EventArgs e)
{
dt.Columns.Add("Name");
dt.Columns.Add("Size");
dt.Columns.Add("Type");
}
protected voidupload_Click(object sender, EventArgs e)
{
if (fileupload1.HasFile)
{
fileupload1.PostedFile.SaveAs(Server.MapPath("~/Files/"+ fileupload1.FileName));
}
foreach(stringfilename in Directory.GetFiles(Server.MapPath("~/Files/")))
{
FileInfo fi=newFileInfo(filename);
DataRow row = dt.NewRow();
row["Name"] = fi.Name;
row["Size"] =fi.Length;
row["Type"] = fi.Extension;
dt.Rows.Add(row);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Download
For download we have to change the response of the click event in rowcommand event of gridview.
C#:
protected voidGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "download")
{
Response.Clear();
Response.ContentType = "Application/octect-stream";
Response.AddHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile("~/Files/"+ e.CommandArgument);
Response.End();
}
}
This article will help you the guys who want to create a code for upload and download the file.
No comments:
Post a Comment