Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTMLInputfile and page behind 1

Status
Not open for further replies.

oakgrove

Programmer
Sep 22, 2002
51
US
I'm trying to use htmlinputfile control with id=&quot;fileup&quot; in an aspx page. The page has an aspx button with id=&quot;Upload&quot; and form tag like this &quot;<Form EncType=&quot;multipart/form-data&quot; runat=&quot;Server&quot;>&quot;. This seems to work fine if I have all in one code file. However, if I use the code behind method, I declare the control in the page behind with &quot;Protected WithEvents fileup As HtmlInputFile&quot;. The page displays ok in the browser and I can use the browse button to pick a file. However when I click the upload button and run the subroutine for it, I get an error on this line &quot;fileup.postedfile.saveas(&quot;new.gif&quot;)&quot;. The error is &quot;Object reference not set to an instance of an object&quot;. And if I look at &quot;fileup&quot; in a debug watch window it says it is set to 'nothing'.

Don't know what I'm doing wrong. Do I need to declare the control differently????

Oakgrove Computer Grouper
Lansing, MI
 
Protected WithEvents fileUp As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Upload As System.Web.UI.WebControls.Button

Private Sub Upload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Upload.Click
If Not (fileUp.PostedFile Is Nothing) Then
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
Dim strPath As String
strPath = Server.MapPath(&quot;your_folder_name&quot;)
strFileNamePath = fileUp.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), &quot;\&quot;)
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
fileUp.PostedFile.SaveAs(strPath & strFileNameOnly)
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top