displaying content of file

Hi Experts,
I have code in C# to upload file to hard disk of the server. I would like to modify it to view the content of the file(text file) that is uploaded. I would appreciate if someone can help me with this. Thanks,

Sudhir

WebForm1.aspx
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="FileUpload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
       <HEAD>
              <title>WebForm1</title>
              <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
              <meta content="C#" name="CODE_LANGUAGE">
              <meta content="JavaScript" name="vs_defaultClientScript">
              <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
       </HEAD>
       <body>
              <form id="Form2" method="post" encType="multipart/form-data" runat="server">
                       <INPUT id="File1" title="Select File To Upload!" style="FONT-SIZE: 9pt; Z-INDEX: 102; LEFT: 76px; WIDTH: 300px; FONT-FAMILY: Arial; POSITION: absolute; TOP: 150px; HEIGHT: 25px" type="file" size="27" name="File1" runat="server">
                     <asp:button id="cmdUpload" style="Z-INDEX: 101; LEFT: 378px; POSITION: absolute; TOP: 150px" runat="server" Text="Upload" Width="90px" Height="25px"></asp:button>
                     <asp:Label id="lblMessage" style="Z-INDEX: 104; LEFT: 76px; POSITION: absolute; TOP: 113px"   runat="server" Width="300px" Height="25px" ForeColor="Red" Font-Bold="True"></asp:Label></form>
       </body>
</HTML>


WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
 
 
namespace FileUpload
{    
       public partial class WebForm1 : System.Web.UI.Page
       {
               string sFileDir= "C:\\";
              long lMaxFileSize = 4096;
     
              private void Page_Load(object sender, System.EventArgs e)
              {
              }
 
              private void DeleteFile (string strFileName)
              {//Delete file from the server
                     if (strFileName.Trim().Length > 0)
                     {
                           FileInfo fi = new FileInfo(strFileName);
                           if (fi.Exists)//if file exists delete it
                           {    
                                  fi.Delete();
                           }
                     }
              }
 
              #region Web Form Designer generated code
              override protected void OnInit(EventArgs e)
              {
                     //
                     // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                     //
                     InitializeComponent();
                     base.OnInit(e);
              }
             
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {  
                     this.cmdUpload.Click += new System.EventHandler(this.cmdUpload_Click);
                     this.Load += new System.EventHandler(this.Page_Load);
 
 
              }
              #endregion
 
 
              private void cmdUpload_Click(object sender, System.EventArgs e)
              {
                   
                     if (( File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
                     {
                           //determine file name
                           string sFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                           try
                           {
                                  if (File1.PostedFile.ContentLength <= lMaxFileSize)
                                  {
                                         //Save File on disk
                                         File1.PostedFile.SaveAs(sFileDir + sFileName);
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File: " + sFileDir + sFileName + " Uploaded Successfully";
                                  }
                                  else //reject file
                                  {
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File Size if Over the Limit of " + lMaxFileSize ;
                                  }
                           }
                           catch (Exception exp)//in case of an error
                           {

                               lblMessage.Visible = true;

                               lblMessage.Text = exp.Message;

                               DeleteFile(sFileDir + sFileName);

                           }


                         
                     }
              }
       }
}




sudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jj819430Commented:
StreamReader is one way

using System.IO;
...
StreamReader SR = new StreamReader(Path);
string Output = "";
while((Output = SR.ReadLine())!= null)
{
..
}
sudAuthor Commented:
Is it possible for you to give me a functioning code. Thanks,

Sudhir
jj819430Commented:
sure
You can actually see it on MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclasstopic.asp

but if you don't want to look there

add using System.IO to your code.

private void Page_Load(object sender, System.EventArgs e)
{
string Path = @"C:\Test.txt"; //put in valid file path
string Output = "";
while((Output = SR.ReadLine())!= null)
{
Response.Write(Output);
}
}
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

sudAuthor Commented:


Hi JJ819430,
 following is the code i am using but it is not working, can u have a look:
the program was working properly earlier i have just used your code along with it.

file: WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
 
 
namespace FileUpload
{    
       public partial class WebForm1 : System.Web.UI.Page
       {
               string sFileDir= "C:\\";
              long lMaxFileSize = 4096;
     
           private void Page_Load(object sender, System.EventArgs e)
              {
                  string Path = @"C:\\mll.txt"; //put in valid file path
string Output = "";
while((Output = SR.ReadLine())!= null)
{
           Response.Write(Output);
}
}
 
              private void DeleteFile (string strFileName)
              {//Delete file from the server
                     if (strFileName.Trim().Length > 0)
                     {
                           FileInfo fi = new FileInfo(strFileName);
                           if (fi.Exists)//if file exists delete it
                           {    
                                  fi.Delete();
                           }
                     }
              }
 
              #region Web Form Designer generated code
              override protected void OnInit(EventArgs e)
              {
                     //
                     // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                     //
                     InitializeComponent();
                     base.OnInit(e);
              }
             
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {  
                     this.cmdUpload.Click += new System.EventHandler(this.cmdUpload_Click);
                     this.Load += new System.EventHandler(this.Page_Load);
 
 
              }
              #endregion
 
 
              private void cmdUpload_Click(object sender, System.EventArgs e)
              {
                   
                     if (( File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
                     {
                           //determine file name
                           string sFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                           try
                           {
                                  if (File1.PostedFile.ContentLength <= lMaxFileSize)
                                  {
                                         //Save File on disk
                                         File1.PostedFile.SaveAs(sFileDir + sFileName);
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File: " + sFileDir + sFileName + " Uploaded Successfully";
                                  }
                                  else //reject file
                                  {
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File Size if Over the Limit of " + lMaxFileSize ;
                                  }
                           }
                           catch (Exception exp)//in case of an error
                           {

                               lblMessage.Visible = true;

                               lblMessage.Text = exp.Message;

                               DeleteFile(sFileDir + sFileName);

                           }


                         
                     }
              }
       }
   




}
jj819430Commented:
What isn't working? is it giving an error?
jj819430Commented:
string Path = @"C:\\mll.txt"; //put in valid file path
string Output = "";
StreamReader SR = new StreamReader(Path);
while((Output = SR.ReadLine())!= null)
{
           Response.Write(Output);
}
}
jj819430Commented:
sorry I had forgotten
StreamReader SR = new StreamReader(Path);
sudAuthor Commented:
error:
system.SR is inaccessible due to its protection level   line 25
system.SR does not contain defination of Readline.   line 25

thanks,

Sudhir
jj819430Commented:
If I may ask, what is this going to be used for? Professional or personal?
sudAuthor Commented:
Hi jj,
   i am trying to read the file that i have just uploaded , what modification i may need to do.

thanks,

sudhir
jj819430Commented:
change this
if (File1.PostedFile.ContentLength <= lMaxFileSize)
                                  {
                                         //Save File on disk
                                         File1.PostedFile.SaveAs(sFileDir + sFileName);
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File: " + sFileDir + sFileName + " Uploaded Successfully";
                                  }
to

if (File1.PostedFile.ContentLength <= lMaxFileSize)
                                  {
                                         //Save File on disk
                                         File1.PostedFile.SaveAs(sFileDir + sFileName);
                                         lblMessage.Visible=true;
                                         lblMessage.Text="File: " + sFileDir + sFileName + " Uploaded Successfully";

string Path = @"sFileDir + sFileName"; //put in valid file path
string Output = "";
StreamReader SR = new StreamReader(Path);
while((Output = SR.ReadLine())!= null)
{
           Response.Write(Output);
}
}


           }

and get rid of it in the page load

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudAuthor Commented:
Hi jj,
   it is going to be used for personal purpose. thanks,

sudhir
sudAuthor Commented:
Hi jj,
 got it to work,

 just changed one line of yours

string Path = @"sFileDir + sFileName"; //put in valid file path

to
string Path = @sFileDir + sFileName; //put in valid file path
and it is working

is it possible to display it in a box ??

Thanks,

sudhir



jj819430Commented:
Sure
Response.Write(Output);
Just puts it in the output.
So you can put output wherever you want.
just remember to append it. (.Text += Output) instead of .Text = Output
sudAuthor Commented:
hi jj,
  i mean display the content of the file in some kind of text box.

Thanks,

Sudhir
jj819430Commented:
sorry, Response.Write writes output to the page.
Output gets set in the while loop.

So you can put output wherever you want.
just remember to append it. (.Text += Output) instead of .Text = Output

Replacing Response.Write(Output)
jj819430Commented:
Add a TextBox to your Page.
call it textBox1

replace Response.Write(Output); with
textBox1.Text += Output;
sudAuthor Commented:
Hi jj,
  thanks for guiding me to complete the program.

Thanks you for your help.

sudhir
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.