Read a textfile from different locations [Resolved]

Posted by Nkkppp under ASP.NET on 9/2/2012 | Points: 10 | Views : 914 | Status : [Member] | Replies : 2
Hi Guys,

I tried to implement the example given in the below path

http://www.aspdotnet-suresh.com/2010/12/how-to-read-and-write-text-file-using.html?showComment=1346600194190#c2307473857763453433

but its not working in Firefox

as the line string path = fileupload1.PostedFile.FileName; returns only filename in firefox, so i have altered the code which saves the file on the server and read the file from there.But still its not working

Below is the code

protected void btnRead_Click(object sender, EventArgs e)
{

string path = "~/Docs/" + fileupload1.FileName;
fileupload1.SaveAs(MapPath(path));
if (File.Exists(Server.MapPath(path)))
{
string[] readText = File.ReadAllLines(path);
StringBuilder strbuild = new StringBuilder();
foreach (string s in readText)
{
strbuild.Append(s);
strbuild.AppendLine();
}
textBoxContents.Text = strbuild.ToString();
}

Please help.

Thanks.

Prathap




Responses

Posted by: Vasanthmvp on: 9/3/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi, i think its working fine dude.
There might be a small mistake.

Here is an example:

.aspx page:

<form id="form1" runat="server">
<div>

<asp:FileUpload ID="fu1" runat="server" />

<asp:Button ID="btn" runat="server" Text="Submit" OnClick="SubmitData" />

<asp:TextBox ID="txt1" runat="server" TextMode="MultiLine" />

</div>
</form>



.aspx.cs file:


protected void SubmitData(object sender, EventArgs e)
{
string path = Server.MapPath("~/");

string filename = fu1.PostedFile.FileName;

if (fu1.HasFile)
{


fu1.SaveAs(path + filename);

if (File.Exists(path + filename))
{


string[] readtext = File.ReadAllLines(path + filename);

StringBuilder strb = new StringBuilder();

foreach (string s in readtext)
{
strb.Append(s);
strb.AppendLine();

}

txt1.Text = strb.ToString();
}
}




}


Regards,

Awesome Coding !! :)

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hey thanks man.

Its working fine. I think earlier there was a mistake in these two lines

string path = "~/Docs/" + fileupload1.FileName;
fileupload1.SaveAs(MapPath(path));

As we need to Map "~/Docs/" as root directory but i had also included the filename.

Thanks dude

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response