|
-
Oct 26th, 2004, 09:36 AM
#1
Thread Starter
Hyperactive Member
[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:
Imports System.IO
Imports System.Data.SqlClient
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFILELINE As String
'+-------------------+
'| INITIALIZE ARRAYS |
'+-------------------+
Dim arLINEREAD() As String
'+-----------------------+
'| OPEN THE FILE TO READ |
'+-----------------------+
Dim objStreamReader As StreamReader
objStreamReader = File.OpenText(TextBox1.Text)
'+-------------------------+
'| READ ONE LINE AT A TIME |
'+-------------------------+
While objStreamReader.Peek() <> -1
'+--------------------------------+
'| PLACE LINE DATA INTO THE ARRAY |
'+--------------------------------+
arLINEREAD = Split(objStreamReader.ReadLine(), ",")
'+--------------------------------+
'| PROCESS LINE TO THE SQL SERVER |
'+--------------------------------+
ADD_ENTRIES(arLINEREAD(0).ToString, arLINEREAD(1).ToString, arLINEREAD(2).ToString, Session.SessionID)
End While
'+--------------+
'| CLOSE OBJECT |
'+--------------+
objStreamReader.Close()
'+--------------------------+
'| DISPLAY INFO TO THE GRID |
'+--------------------------+
With DataGrid1
.CurrentPageIndex = 0
.DataSource = GetVERIZONList(Session.SessionID)
.DataBind()
.Visible = True
End With
End Sub
End Class
Last edited by ARPRINCE; Oct 26th, 2004 at 04:01 PM.
-
Oct 26th, 2004, 09:50 AM
#2
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.
-
Oct 26th, 2004, 10:06 AM
#3
Thread Starter
Hyperactive Member
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
-
Oct 26th, 2004, 10:11 AM
#4
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()
-
Oct 26th, 2004, 03:58 PM
#5
Thread Starter
Hyperactive Member
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:
'-- HTML CODE PART
<asp:label id="Label2" runat="server">Select a file to upload: </asp:label>
<input id="uploadedFile" type="file" name="uploadedFile" runat="server">
'-- VB CODE PART
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'+----------------------------------------+
'| WE NEED TO UPLOAD TO IIS TO THE SERVER |
'+----------------------------------------+
Dim strfilename As String = uploadedFile.PostedFile.FileName
myFileName = System.IO.Path.GetFileName(strfilename)
uploadedFile.PostedFile.SaveAs(Server.MapPath("\RA\TEMP\" & Session.SessionID & myFileName))
'+------------------------+
'| LET'S PROCESS THE FILE |
'+------------------------+
Call PROCESS_THE_FILE()
End Sub
Private Sub PROCESS_THE_FILE()
'+-----------------------------------------+
'| INITIALIZE AN ARRAY AND PLACE ALL FILES |
'| FROM THAT DIRECTORY INSIDE THE ARRAY. |
'+-----------------------------------------+
Dim arLINEREAD() As String
'+---------------+
'| READ THE FILE |
'+---------------+
Dim fsSTREAM As New FileStream(Server.MapPath("\RA\TEMP\" & Session.SessionID & myFileName), FileMode.Open, FileAccess.Read)
Dim srREADER As New StreamReader(fsSTREAM)
'+-------------------------------+
'| PROCESS THE FILE LINE BY LINE |
'+-------------------------------+
While srREADER.Peek() > -1
arLINEREAD = Split(srREADER.ReadLine(), ",")
'+--------------------------------+
'| PROCESS LINE TO THE SQL SERVER |
'+--------------------------------+
ADD_ENTRIES(arLINEREAD(0).ToString, arLINEREAD(1).ToString, arLINEREAD(2).ToString, Session.SessionID)
End While
srREADER.Close()
fsSTREAM.Close()
'+--------------------------+
'| DISPLAY INFO TO THE GRID |
'+--------------------------+
With DataGrid1
.CurrentPageIndex = 0
.DataSource = GetVerizonList(Session.SessionID)
.DataBind()
.Visible = True
End With
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|