Click here to Skip to main content
Licence 
First Posted 24 Apr 2002
Views 87,706
Bookmarked 25 times

Save binary data to SQL Server

By Fernando Finelli | 25 Apr 2002
ASP can save binary data to a table in SQL Server
1 vote, 33.3%
1

2

3
2 votes, 66.7%
4

5
3.23/5 - 12 votes
μ 3.23, σa 3.03 [?]

Introduction

First install SoftArtisans.FileUp component

to upload the files. Or you can use any other file upload component that you have access to.

Then, create the table into SQL Server

create table test (
file_id int identity(1,1),
file_type varchar(50),
file_name varchar(50),
file_data image,
date_created smalldatetime)

Create the ASP File (upload.asp) to save the file into SQL Server

Html Code

<form ENCTYPE="multipart/form-data" METHOD="post" 
    ACTION="upload.asp" name="frmUpload"> 
<input type="file" size="15" src="" width="67" height="18" 
    id="FILE1" NAME="FILE1" value="Search"> 
<input TYPE="submit" NAME="btnEnviar" VALUE="Submit">
</form>

ASP Code

Dim objUpload
Set objUpload = Server.CreateObject("SoftArtisans.FileUp")

Set MSCS = Server.CreateObject("ADODB.Connection")
MSCS .ConnectionTimeout = 90
MSCS .Open "DSN=DSN_NAME;UID=USERNAME;PWD=PASSWORD;DATABASE=DATABASE_NAME"

If objUpload.Form("FILE1") <> "" Then
    Set Rs = Server.CreateObject ("ADODB.Recordset")
    Rs.Open "test", MSCS, 2, 3

    Rs.AddNew 

    Rs("file_name") = objUpload.Form("FILE1").UserFilename
    objUpload.Form("FILE1").SaveAsBlob Rs("file_data")    
    Rs("file_type") = objUpload.Form("FILE1").contentType
    Rs("date_created") = now()
    
    Rs.upDate()

    Rs.Close 

    Response.Write "Uploaded Successfully"
End If    
Set objUpload = Nothing
%>

Now create the view_upload.asp to view the file

<%
Set MSCS= Server.CreateObject("ADODB.Connection")
MSCS.ConnectionTimeout = 90
MSCS.Open "DSN=DSN_NAME;UID=USERNAME;PWD=PASSWORD;DATABASE=DATABASE_NAME"

Set Rs = Server.CreateObject ("ADODB.Recordset")
strSQL = "SELECT * FROM test WHERE file_id = " & request("id")
Rs.Open strSQL, MSCS, 2, 3

Response.ContentType = Rs("file_type")
Response.BinaryWrite Rs("file_data")
Rs.close
%>
To view the file saved number 1 browse to http://server.com/view_upload.asp?id=1

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Fernando Finelli

Web Developer

Argentina Argentina

Member
Lic. Fernando Finelli

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSQL Column DataType PinmemberKarthick_Accet10:56 2 Jan '09  
GeneralCool Man Pinmembercoolnaveen12341:11 20 Apr '06  
QuestionPlease give me a way for display pictures which store in DB Access 2003 by "OLE object"? Pinmembertuyenhnp17:52 25 Sep '05  
Generalcant get to connect with SQL server 2000 Pinmemberjaf5:30 16 Jan '04  
GeneralRe: cant get to connect with SQL server 2000 PinmemberWiB6:28 16 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 26 Apr 2002
Article Copyright 2002 by Fernando Finelli
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid