Monday, January 12, 2009

Reading a text file to a remote PC

Put a file upload control in your page give id like "FileUpload1".
create a folder in your application like "TxtFile" folder name.
First save file in folder and then read the file
after reading delete the file.

StreamReader sr;
string sfileExtension = System.IO.Path.GetExtension(sFileName);
string sFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("TxtFile") + "\\" + sFileName);

try
{
sr = new StreamReader(Server.MapPath("TxtFile") + "\\" + sFileName);

StringBuilder NameList = new StringBuilder();
NameList.Append(sr.ReadToEnd());
string info = NameList.ToString();
sr.Close();
File.Delete(Server.MapPath("TxtFile") + "\\" + sFileName);
}
catch (Exception ex)
{
Response.Write("File Reading Failed Due to : " + ex.Message.ToString());
}

No comments: