Upload and Read Flat Files[.txt] Using File Upload in Gridview Using C#.Net
In this Post I will show you how to read the content of Flat files by uploading and showing it inside a Gridview..
Refer the Code Snippet Below:
Design Part:
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Upload and Display in Gridview</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" /> <hr /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded"> <Columns> <asp:BoundField DataField="Text" HeaderText="File Name" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lnkView" Text = "View" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "ViewFile"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Label ID="lblMsg" runat="server" Font-Bold="True" Font-Italic="True" ForeColor="#00C000"></asp:Label> <br /> <br /> <asp:Button ID="btnClear" runat="server" OnClick="btnClear_Click" Text="Clear" /></div> </form> </body> </html>
Code-Behind:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/")); List<ListItem> files = new List<ListItem>(); foreach (string filePath in filePaths) { files.Add(new ListItem(Path.GetFileName(filePath), filePath)); } GridView1.DataSource = files; GridView1.DataBind(); } } protected void UploadFile(object sender, EventArgs e) { lblMsg.Text = ""; string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName); Response.Redirect(Request.Url.AbsoluteUri); } protected void DownloadFile(object sender, EventArgs e) { lblMsg.Text = ""; string filePath = (sender as LinkButton).CommandArgument; Response.ContentType = ContentType; Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath)); Response.WriteFile(filePath); Response.End(); } protected void DeleteFile(object sender, EventArgs e) { lblMsg.Text = ""; string filePath = (sender as LinkButton).CommandArgument; File.Delete(filePath); Response.Redirect(Request.Url.AbsoluteUri); } protected void ViewFile(object sender, EventArgs e) { string filePath = (sender as LinkButton).CommandArgument; string inputString; lblMsg.Text = ""; using (StreamReader streamReader = File.OpenText(filePath)) { inputString = streamReader.ReadLine(); while (inputString != null) { lblMsg.Text += inputString + "<br />"; inputString = streamReader.ReadLine(); } } } protected void btnClear_Click(object sender, EventArgs e) { lblMsg.Text = ""; } }
Really a great article to Read Flat Files...Thanks @Vishal
ReplyDelete