button click clears textbox and returns null error

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.
 
G

Guest

There is a default limit to the file size (2 MB) that can be uploaded. If you need to upload a file or more than the defaul then you can over ride it in the Web.Config file.

Something like the
<?xml version="1.0" encoding="utf-8" ?><configuration><system.web><!-- DYNAMIC DEBUG COMPILATIO
<httpRuntime maxRequestLength="20480"/
20MB * 1024 Max File Site has been Set Her
--

20 MB - Yeah this site allows for huge file uploads. Engg Diagrams in PDF Format :

HT

Trevor Benedict
MCS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top