In this article I am going to explain how to upload and read text
file in asp.net.
In previous article I have explained Upload and read worddocument in asp.net, how to get MAC address of system using asp.net, how toselect records after a specific record/Id in sql server and how to generatelimited random number and add hyphen after each 4th character in asp.net.
Description:
I want to upload and read the text file. Text file will be
save to folder and content of text file will be displayed in textbox.
Implementation:
HTML
markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>Upload File :</td><td><asp:FileUpload ID="FileUpload1" runat="server" accept=".txt" /></td></tr>
<tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Button" /></td></tr>
<tr><td colspan="2"></td></tr>
<tr><td colspan="2"><asp:TextBox ID="txtarea" runat="server" TextMode="MultiLine" Width="600px" Height="500px" Visible="false"></asp:TextBox></td></tr>
</table>
</div>
</form>
</body>
</html>
Add
namespace
C# Code:
using System.IO;
VB.net Code:
Imports System.IO
On button click write the below given code
C# Code:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string filepath = Server.MapPath("~/upload") + Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(filepath);
string text = File.ReadAllText(filepath);
txtarea.Visible = true;
txtarea.Text = text;
}
catch (Exception ex) { }
}
VB.net Code:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim filepath As String = Server.MapPath("~/upload ") + Path.GetFileName(FileUpload1.FileName)
FileUpload1.SaveAs(filepath)
Dim text As String = File.ReadAllText(filepath)
txtarea.Visible = True
txtarea.Text = text
Catch ex As Exception
End Try
End
Sub End
Sub
can you send me all this code in zip file?
ReplyDelete