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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading image files in form

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
Since we can only have 1 form per aspx file, is it possible to include a file upload process on the same form as other webcontrols? Thanks regards,
Brian
 
Brian. Not sure if this will help, but I would think that if you have a Subroutine (as below) in your codebehind it should be enough, and shouldn't be in anyway incompatible with the rest of your code. Just a few lines of code (uploads an image and sticks it into an Access table):

Sub DoUpload(Sender As Object, e As System.EventArgs)

Dim sPath as String
Dim sFile as String
Dim sFullPath as String
Dim sSplit() as String
Dim sPathFriendly as String

'Upload to same path as script
'Internet Anonymous User must have write permissions

sPath = Server.MapPath(".\Photos")
If Right(sPath, 1) <> &quot;\&quot; then
sPathFriendly = sPath 'Friendly path name for display
sPath = sPath & &quot;\&quot;
Else
sPathFriendly = Left(sPath, Len(sPath) - 1)
End If

'Save as same file name being posted
'The code below resolves the file name
'(removes path info)
sFile = txtUpload.PostedFile.FileName
sSplit = Split(sFile, &quot;\&quot;)
sFile = sSplit(Ubound(sSplit))
sFullPath = sPath & sFile
Try
txtUpload.PostedFile.SaveAs(sFullPath)
Catch Ex as Exception
lblResults.Text = &quot;Upload of File &quot; & sFile & &quot; to &quot; & sPathFriendly & &quot; failed for the following reason: &quot; & Ex.Message
Finally
'...
End Try

'and now stick the image in the dbase table...
'here I am sticking a comment field and an image...
Dim strString As String
strString = txtMemo.Text
strString = strSTring.Replace(&quot;'&quot;,&quot;''&quot;) 'replace apos
Dim dbconn As OleDbConnection
Dim DBCommand As OleDBDataAdapter
Dim DBInsert As New OleDbCommand
'Open the database connection
dbconn = New OleDbConnection( _
&quot;Provider=Microsoft.Jet.OLEDB.4.0; &quot; & _
&quot;Data Source=&quot; & Server.MapPath (&quot;.\fpdb\NewRecords.mdb;&quot;))
DBInsert.CommandText = &quot;INSERT INTO tblPhotos (Comments, AwwSiteCode) VALUES ('&quot; & strString & &quot;', '&quot; & Me.lblID.Text & &quot;')&quot;
DBInsert.Connection = dbconn
DBInsert.Connection.Open
DBInsert.ExecuteNonQuery()
DBInsert.Connection.Close
Response.Redirect(&quot;.\PHotoConfirm.aspx?AwwSiteCode=&quot; & MyID)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top