Results 1 to 5 of 5

Thread: [RESOLVED] Error Reading Text File From Client PC

  1. #1

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Resolved [RESOLVED] Error Reading Text File From Client PC

    I have the code below. On my web page, I have a textbox and a
    button. User enters the filename and path (ex. c:\test.txt) on the textbox and clicks on the button to process the file.

    If I run it on the machine hosting the site, I have no errors.
    However, when I run it on a client PC, I get an error. It's not
    reading the file entered into the textbox.

    Am I missing anything here? Why would it not read the text file in the client PC?

    TIA


    VB Code:
    1. Imports System.IO
    2. Imports System.Data.SqlClient
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.         Dim strFILELINE As String
    7.         '+-------------------+
    8.         '| INITIALIZE ARRAYS |
    9.         '+-------------------+
    10.         Dim arLINEREAD() As String
    11.  
    12.         '+-----------------------+
    13.         '| OPEN THE FILE TO READ |
    14.         '+-----------------------+
    15.         Dim objStreamReader As StreamReader
    16.         objStreamReader = File.OpenText(TextBox1.Text)
    17.  
    18.         '+-------------------------+
    19.         '| READ ONE LINE AT A TIME |
    20.         '+-------------------------+
    21.         While objStreamReader.Peek() <> -1
    22.             '+--------------------------------+
    23.             '| PLACE LINE DATA INTO THE ARRAY |
    24.             '+--------------------------------+
    25.             arLINEREAD = Split(objStreamReader.ReadLine(), ",")
    26.             '+--------------------------------+
    27.             '| PROCESS LINE TO THE SQL SERVER |
    28.             '+--------------------------------+
    29.             ADD_ENTRIES(arLINEREAD(0).ToString, arLINEREAD(1).ToString, arLINEREAD(2).ToString, Session.SessionID)
    30.         End While
    31.  
    32.         '+--------------+
    33.         '| CLOSE OBJECT |
    34.         '+--------------+
    35.         objStreamReader.Close()
    36.  
    37.         '+--------------------------+
    38.         '| DISPLAY INFO TO THE GRID |
    39.         '+--------------------------+
    40.         With DataGrid1
    41.             .CurrentPageIndex = 0
    42.             .DataSource = GetVERIZONList(Session.SessionID)
    43.             .DataBind()
    44.             .Visible = True
    45.         End With
    46.  
    47.     End Sub
    48. End Class
    Last edited by ARPRINCE; Oct 26th, 2004 at 04:01 PM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    You cant do that. You cant read/write files on a client from a server. You would need to upload the file to read it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Thanks for your response.

    Would that mean I need to create a shared directory in my IIS server for my file uploads?

    I'm thinking of adding an UPLOAD button. When the user click on the button, it first uploads the entered file from the client to the server then process it after. But how would I do an UPLOAD?

    Thanks

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    You can use the file uplaod web form control and the follwoing code should get you started

    Code:
    Dim s As System.IO.Stream = File1.PostedFile.InputStream
    			Dim data(File1.PostedFile.ContentLength - 1) As Byte
    			s.Read(data, 0, File1.PostedFile.ContentLength)
    			' Create file on the server
    			Dim fs As System.IO.FileStream = New System.IO.Filestream(Server.Mappath("Uploads\temp.ful"), System.IO.FileMode.Create, System.IO.FileAccess.Write)
    			fs.Write(data, 0, File1.PostedFile.ContentLength - 1)
    			fs.Close()
    			s.Close()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Resolved

    Thank you for your tip. I also did a search on the forum and finally came with this solution based on one of the thread.

    So I basically have that ASP code that gives me the file explorer
    option on the web page so I got rid of the textbox web control.

    VB Code:
    1. '-- HTML CODE PART
    2. <asp:label id="Label2" runat="server">Select a file to upload: </asp:label>
    3. <input id="uploadedFile" type="file" name="uploadedFile" runat="server">
    4.  
    5. '-- VB CODE PART
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         '+----------------------------------------+
    8.         '| WE NEED TO UPLOAD TO IIS TO THE SERVER |
    9.         '+----------------------------------------+
    10.         Dim strfilename As String = uploadedFile.PostedFile.FileName
    11.         myFileName = System.IO.Path.GetFileName(strfilename)
    12.         uploadedFile.PostedFile.SaveAs(Server.MapPath("\RA\TEMP\" & Session.SessionID & myFileName))
    13.  
    14.         '+------------------------+
    15.         '| LET'S PROCESS THE FILE |
    16.         '+------------------------+
    17.         Call PROCESS_THE_FILE()
    18.     End Sub
    19.  
    20.     Private Sub PROCESS_THE_FILE()
    21.         '+-----------------------------------------+
    22.         '| INITIALIZE AN ARRAY AND PLACE ALL FILES |
    23.         '| FROM THAT DIRECTORY INSIDE THE ARRAY.   |
    24.         '+-----------------------------------------+
    25.         Dim arLINEREAD() As String
    26.  
    27.         '+---------------+
    28.         '| READ THE FILE |
    29.         '+---------------+
    30.         Dim fsSTREAM As New FileStream(Server.MapPath("\RA\TEMP\" & Session.SessionID & myFileName), FileMode.Open, FileAccess.Read)
    31.         Dim srREADER As New StreamReader(fsSTREAM)
    32.  
    33.         '+-------------------------------+
    34.         '| PROCESS THE FILE LINE BY LINE |
    35.         '+-------------------------------+
    36.         While srREADER.Peek() > -1
    37.             arLINEREAD = Split(srREADER.ReadLine(), ",")
    38.             '+--------------------------------+
    39.             '| PROCESS LINE TO THE SQL SERVER |
    40.             '+--------------------------------+
    41.             ADD_ENTRIES(arLINEREAD(0).ToString, arLINEREAD(1).ToString, arLINEREAD(2).ToString, Session.SessionID)
    42.         End While
    43.         srREADER.Close()
    44.         fsSTREAM.Close()
    45.  
    46.         '+--------------------------+
    47.         '| DISPLAY INFO TO THE GRID |
    48.         '+--------------------------+
    49.         With DataGrid1
    50.             .CurrentPageIndex = 0
    51.             .DataSource = GetVerizonList(Session.SessionID)
    52.             .DataBind()
    53.             .Visible = True
    54.         End With
    55.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width