Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 652
Overwriting Files Stored in Database
How to overwrite an existing file stored in a database.
To overwrite a file stored in a relational database, you must first populate an ADO Recordset object with the row that contains the file. It is important to select the BLOB data from the row, as well as any other information you wish to update. You can change the value of the non-BLOB data using the syntax: Recordset.Fields(<fieldname>).Value = <newvalue>. Next, you call the SaveAsBlob method and pass in the BLOB field. This will overwrite what is in that row of the database, and your new file will be saved. To finalize your changes, call the Update method of the Recordset object.

Following is code that illustrates this:


'--- Create FileUp Object
set upl = Server.CreateObject("SoftArtisans.FileUp")

'--- Set temporary Path for FileUp Object
upl.Path = "C:\uploads"

'--- Create ADO Connection
set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("Your Connection String Here")

'--- Create ADO Recordset
set oRS = Server.CreateObject("ADODB.Recordset")

'--- Build SQL Statement to select file from database
strSQL = "SELECT filename, filebinary FROM tblUpload "
strSQL = strSQL & "WHERE fileid = " & upl.Form("FILEID")

'--- Retrieve single row in Database with the file you want to replace
oRS.Open(strSQL, oConn, 2, 3)

'--- OPTIONAL: Set the filename field equal to the filename of the user's upload.
'--- If you are replacing a file, it should be the same filename.
oRS.Fields("filename").Value = upl.Form("FILE1").ShortFilename

'--- SaveAsBlob will overwrite the existing file
upl.Form("FILE1").SaveAsBlob oRS.Fields("filebinary")

'--- Commit changes

'--- Cleanup objects
set oRS = nothing
set oConn = nothing
set upl = nothing

Created : 6/24/2003 2:37:59 PM (last modified : 6/24/2003 2:37:57 PM)
Rate this article!