I have a file upload control on my webform. When I want to use it to select a text file. I will not be uploaded the file however, I will need the value of the path so I can open the text file with the streamreader. I then will pass the value of the text
file to a database. How do I get the value of the fileupload textbox?
Here is my code so far:
Dim FILENAME As String = NEED VALUE OF FILEUPLOAD TEXTBOX VALUE HERE
Dim myfile As String
Dim objStreamReader As StreamReader
objStreamReader = File.OpenText(myfile.Text)
Dim contents As String = objStreamReader.ReadToEnd()
t.Text = contents
objStreamReader.Close()
If I was helpful, please mark "answered" so I can get credit. Thanks!
the full path for the users file can be seen in the property: FileUpload.PostedFile.FileName
Dim userPath As String
If Not Me.FileUpload1.PostedFile Is Nothing Then
userPath = Me.FileUpload1.PostedFile.FileName
End If
But if you can read this property, then the file has been uploaded. Also, you cannot use this path to read the file directly from the users file system with a streamreader. When you use this path on the server, it will refer to your server file system
and not the clients file system.
You can read the uploaded file using:
Me
.FileUpload1.PostedFile.InputStream
Mike Banavige
Marked as answer by Bonekrusher on Jun 17, 2007 11:49 PM
Bonekrusher
Contributor
4027 Points
922 Posts
Get Full Value of Fileupload path
Jun 17, 2007 08:27 PM|LINK
Hi,
I have a file upload control on my webform. When I want to use it to select a text file. I will not be uploaded the file however, I will need the value of the path so I can open the text file with the streamreader. I then will pass the value of the text file to a database. How do I get the value of the fileupload textbox?
Here is my code so far:
Dim FILENAME As String = NEED VALUE OF FILEUPLOAD TEXTBOX VALUE HERE Dim myfile As String Dim objStreamReader As StreamReader objStreamReader = File.OpenText(myfile.Text) Dim contents As String = objStreamReader.ReadToEnd() t.Text = contents objStreamReader.Close()mbanavige
All-Star
134967 Points
15422 Posts
ASPInsiders
Moderator
MVP
Re: Get Full Value of Fileupload path
Jun 17, 2007 09:03 PM|LINK
the full path for the users file can be seen in the property: FileUpload.PostedFile.FileName
Dim userPath As String
If Not Me.FileUpload1.PostedFile Is Nothing Then
userPath = Me.FileUpload1.PostedFile.FileName
End If
But if you can read this property, then the file has been uploaded. Also, you cannot use this path to read the file directly from the users file system with a streamreader. When you use this path on the server, it will refer to your server file system and not the clients file system.
You can read the uploaded file using:
Me
.FileUpload1.PostedFile.InputStreamBonekrusher
Contributor
4027 Points
922 Posts
Re: Get Full Value of Fileupload path
Jun 17, 2007 11:49 PM|LINK
Thanks you, you have been a great help.
My work around was to upload the file to the server, read it with the streamreader and then delete the file.
I will try "Me.FileUpload1.PostedFile.FileName" to read the file directly.
Thanks!
danishjee
Member
213 Points
714 Posts
Re: Get Full Value of Fileupload path
Jul 06, 2008 04:16 PM|LINK
how can i assign my path run time to fileupload
for example
if fileupload1.PostedFile is nothing then
get the file "~/Imgs/NoImage.jpg" and assing to fileupload.
end if
how can i do this thing
touseefahmad
Member
6 Points
3 Posts
Re: Get Full Value of Fileupload path
Sep 18, 2010 05:44 AM|LINK
Thanks a lot Mike. Your answer was very helpful.
Regards,
Touseef Ahmad