Design
<strong> Upload File</strong><asp:FileUpload ID="txtfileUpload" runat="server"/><asp:Button ID="btnViewFile" runat="server" Text="Add File"onclick="btnViewFile_Click" /><span><asp:TextBox ID="txtMain" runat="server" TextMode="MultiLine"></asp:TextBox>
</span>
Note : Make a new folder named File ( or any as per choice ) in the solution explorer for the project.
Now on .cs File do the following code on button click (btnViewFile)
.cs File
protected void btnViewFile_Click(object sender, EventArgs e){
if (txtfileUpload.HasFile){string fileName = txtfileUpload.PostedFile.FileName;string pathToSave = "~/File";txtfileUpload.PostedFile.SaveAs(Server.MapPath(pathToSave) + fileName);string Filepath = Server.MapPath(pathToSave + fileName);
StreamReader reader = new StreamReader(Filepath);string text = reader.ReadToEnd();txtMain.Text = text;
}
Code Explanation :
The if condition is checking for the availability of file.
After that we are saving the file by taking the "Server.MapPath" method in bracket we have given the path with the name of the file.
StreamReader is used to read the file.
No comments:
Post a Comment