T
Tim Zych
I am trying to follow the instructions from
http://authors.aspalliance.com/das/tutorial/fileupload.aspx to upload a
file.
The webform contains 2 buttons and a textbox. Browse for a file, click the
Upload button and it's supposed to save it somewhere.
Problem is, when browse for a file and click the Upload button, the textbox
get cleared out and the page refreshes. No save action occurs.
Here's the code:
Sub Upload(ByVal Source As Object, ByVal e As EventArgs)
If Not (myFile.PostedFile Is Nothing) Then
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
'Logic to find the FileName (excluding the path)
strFileNamePath = myFile.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, _
(Len(strFileNamePath) - intFileNameLength) + 2)
myFile.PostedFile.SaveAs _
("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
lblMsg.Text = "File Upload Success."
End If
End Sub
with the SaveAs location changed for my system.
I doubt the code has a problem. If I comment out the "If Not
(myFile.PostedFile Is Nothing) Then" I get an
error:
"Object reference not set to an instance of an object"
and the "strFileNamePath = myFile.PostedFile.FileName" is highlighted.
The textbox is then cleared out. Why in the world is the textbox getting
cleared out every time?
Last time something like this happened it was because I turned Viewstate
off. However, enableViewstate is True for this webform.
Anybody know why the textbox is getting cleared out every time?
Thanks.
http://authors.aspalliance.com/das/tutorial/fileupload.aspx to upload a
file.
The webform contains 2 buttons and a textbox. Browse for a file, click the
Upload button and it's supposed to save it somewhere.
Problem is, when browse for a file and click the Upload button, the textbox
get cleared out and the page refreshes. No save action occurs.
Here's the code:
Sub Upload(ByVal Source As Object, ByVal e As EventArgs)
If Not (myFile.PostedFile Is Nothing) Then
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
'Logic to find the FileName (excluding the path)
strFileNamePath = myFile.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, _
(Len(strFileNamePath) - intFileNameLength) + 2)
myFile.PostedFile.SaveAs _
("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
lblMsg.Text = "File Upload Success."
End If
End Sub
with the SaveAs location changed for my system.
I doubt the code has a problem. If I comment out the "If Not
(myFile.PostedFile Is Nothing) Then" I get an
error:
"Object reference not set to an instance of an object"
and the "strFileNamePath = myFile.PostedFile.FileName" is highlighted.
The textbox is then cleared out. Why in the world is the textbox getting
cleared out every time?
Last time something like this happened it was because I turned Viewstate
off. However, enableViewstate is True for this webform.
Anybody know why the textbox is getting cleared out every time?
Thanks.