Hi,
I'm using Visual Studio 2005. I am developing a web application that uses
the FileUpload control to upload text files to a directory on a webserver.
This works fine.
However, for me to be able to further process the files, I need to insert
some data at the BEGINNING of the text file. All the solutions I've seen
either append the text to the end of the file, or overwrite the data at the
start of the file, or read the entire file into a string
("StreamReader.ReadToEnd"), concatenate, then write the string back to the
file (innefficient).
Please note:
The text files in question could be up to 5000 lines long with each line
being up to 200 characters long (+- 1 million chars), and storing all that in
a string may be a strain on resources, so the solution must be efficient.
Isn't there a way to directly access the file to do the insert?? Something
like:
1. Convert text file to a "direct access file"
2. Insert data at the begining
3. Convert "direct access file" back to a text file
So far my code is as follows for the submit button on the page (see my
commented lines):
Imports System
Imports System.IO
....
Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles butSubmit.Click
.... (working code)
If FileOK Then
Try
Dim strNewFileName As String = path & Now().Ticks.ToString &
"NewFileName.txt"
Dim strRefNo As String = "1234567890"
Me.FileUpload1.PostedFile.SaveAs(strNewFileName)
'The text file now contains:
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.
'this is where I need a code snippet .....
'----
'---- INSERT strRefNo at the BEGINNING of the text file represented by
strNewFileName ----
'----
'After the insert, the text file must be on the webserver and now contain:
'1234567890
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.
Me.lblResult.ForeColor = Drawing.Color.Black
Me.lblResult.Text = "File successfully uploaded!"
Catch ex As Exception
Me.lblResult.ForeColor = Drawing.Color.Red
Me.lblResult.Text = "File could not be uploaded."
End Try
Else
'display error message
Me.lblResult.ForeColor = Drawing.Color.Red
Me.lblResult.Text = "Cannot accept files of this type."
End If 'FileOK
.... (working code)
End Sub
Any help will be appreciated.