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

Saving image file to sql server

Status
Not open for further replies.

NeedVBHelp

Programmer
Mar 9, 2000
15
US
I try to make an asp/html page that allow user to upload file to blob field in sql server.

please help.

thanks

tvuong tvuong@kempercm.com
Programmer.
 
the html would look something like:
<FORM METHOD=&quot;POST&quot; ACTION=&quot;Sample4.asp&quot; ENCTYPE=&quot;multipart/form-data&quot;>
<INPUT TYPE=&quot;FILE&quot; NAME=&quot;FILE1&quot; SIZE=&quot;50&quot;><BR>
<INPUT TYPE=&quot;FILE&quot; NAME=&quot;FILE2&quot; SIZE=&quot;50&quot;><BR>
<INPUT TYPE=&quot;FILE&quot; NAME=&quot;FILE3&quot; SIZE=&quot;50&quot;><BR>
<INPUT TYPE=&quot;FILE&quot; NAME=&quot;FILE4&quot; SIZE=&quot;50&quot;><BR>
<INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Upload&quot;>
</FORM>

But you need a component to call in Sample4.asp to process the upload. Here's the Sample4.asp code:

<HTML>
<BODY BGCOLOR=&quot;white&quot;>

<H1>aspSmartUpload : Sample 4</H1>
<HR>

<%
' Variables
' *********
Dim mySmartUpload
Dim file
Dim oConn
Dim oRs
Dim intCount
intCount=1

' Object creation
' ***************
Set mySmartUpload = Server.CreateObject(&amp;quot;aspSmartUpload.SmartUpload&amp;quot;)

' Upload
' ******
mySmartUpload.Upload

' Connect to the DB
' *****************
Set oConn = openConnection( )
'curDir = Server.MapPath(&amp;quot;Sample.mdb&amp;quot;)
'oConn.Open &amp;quot;DBQ=&amp;quot;&amp;amp; curDir &amp;amp;&amp;quot;;Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS Access;&amp;quot;

' Open a recordset
' ****************
strSQL = &amp;quot;SELECT RecordID, FileName, Type, [File] FROM tblFiles &amp;quot;

Set oRs = Server.CreateObject(&amp;quot;ADODB.recordset&amp;quot;)
'Set oRs.ActiveConnection = oConn
'oRs.Source = strSQL
oRs.LockType = 3

Call oRs.Open( strSQL, oConn, adOpenDynamic, adLockPessimistic )

' Select each file
' ****************
For each file In mySmartUpload.Files
' Only if the file exist
' **********************
If not file.IsMissing Then

' Add the current file in a DB field
' **********************************
oRs.AddNew
file.FileToField oRs.Fields(&amp;quot;File&amp;quot;)
oRs(&amp;quot;FileName&amp;quot;) = file.FileName
oRS( &amp;quot;Type&amp;quot; ) = &amp;quot;tst&amp;quot;
oRS( &amp;quot;RecordID&amp;quot; ) = intCount
oRs.Update
intCount = intCount + 1
End If
Next

' Display the number of files uploaded
' ************************************
Response.Write(intCount &amp;amp; &amp;quot; file(s) uploaded.<BR>&amp;quot;)

' Destruction
' ***********
oRs.Close
oConn.Close
Set oRs = Nothing
Set oConn = Nothing
%>
</BODY>
</HTML>


<%
function openConnection()
Dim objConnection

set objConnection = Server.CreateObject(&amp;quot;ADODB.Connection&amp;quot;)
objConnection.Provider = &amp;quot;SQLOLEDB&amp;quot;
objConnection.Open &amp;quot;Source=dev1;UID=sa;PWD=;Initial Catalog=bcc1-military;&amp;quot;

' objConnection.Open Application(&amp;quot;ConnectionString&amp;quot;)

set openConnection = objConnection
end function
%>
The above is a quickie but you should really use sp's to talk to the db.

I recommend the SmartASP component only because it is free !

JAC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top