Hi indradeo,
Check this example. Now please take its reference and correct your code.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="OnClickUpload"/>
<br />
<asp:Label ID="lblText" runat="server" />
Namespaces
C#
using System.IO;
using System.Text;
VB.Net
Imports System.IO
Imports System.Text
Code
C#
protected void OnClickUpload(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
if (!Directory.Exists(Server.MapPath("~/Files")))
{
Directory.CreateDirectory(Server.MapPath("~/Files"));
}
string path = Server.MapPath("~/Files/" + fileName);
FileUpload1.SaveAs(path);
if (File.Exists(path))
{
string[] texts = File.ReadAllLines(path);
StringBuilder sb = new StringBuilder();
foreach (string text in texts)
{
sb.Append(text);
sb.Append("<br/>");
}
lblText.Text = sb.ToString();
}
}
VB.Net
Protected Sub OnClickUpload(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
If Not Directory.Exists(Server.MapPath("~/Files")) Then
Directory.CreateDirectory(Server.MapPath("~/Files"))
End If
Dim path As String = Server.MapPath("~/Files/" & fileName)
FileUpload1.SaveAs(path)
If File.Exists(path) Then
Dim texts As String() = File.ReadAllLines(path)
Dim sb As StringBuilder = New StringBuilder()
For Each text As String In texts
sb.Append(text)
sb.Append("<br/>")
Next
lblText.Text = sb.ToString()
End If
End Sub
Screenshot