Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 379
Product
FileUp
Title
Upload to Two Locations Simultaneously
Solution

Some applications require saving an uploaded file in more than one directory, or in a directory and a database. This can be accomplished easily using SoftArtisans FileUp in conjunction with SoftArtisans FileManager, which you received free with your purchase of FileUp. If you attempt to do this with FileUp alone, it will not work, as using FileUp's Save method more than once will still result in only one file on the server (at the last location specified). This is because the file is already cached and, for performance reasons, the Save method simply renames the file rather than writing it twice.

The solution is to copy the uploaded file using FileManager's CopyFile method. You can copy to as many locations as desired. If you want to copy the file to a separate server which is networked to the Web server, use CopyFile with a UNC path. Note: Uploading to a different server involves special security considerations.

Here is an example of saving to two directories:

<%
'---
'--- Instantiate FileUp.
'---
set upl = Server.CreateObject("SoftArtisans.FileUp")
'---
'--- Set the location for the temporary cache of the upload.
'--- This property must be set before referencing any form elements.
'---
upl.path = "c:\temp"
'---
'--- Extract the user's original filename from the
'--- full path on the client.
'---
strFilename = Mid(upl.UserFilename, InstrRev(upl.UserFilename, "\") + 1)
'---
'--- Save the file to the first directory with the user's original
'--- filename.
upl.SaveAs "c:\firstDirectory\" & strFilename
'---
'--- Instantiate FileManager.
'---
set oFM = Server.CreateObject("SoftArtisans.FileManager")
'---
'--- Copy the file to the second directory with the user's original
'--- filename.

oFM.CopyFile upl.ServerName, "c:\secondDirectory\" & strFilename
'---
'--- Clean up.
'---
'---
Set oFM = Nothing
Set upl = Nothing
%>

If you need to have one copy of a file in a database as well as a directory, we would recommend saving the file to the directory first, and then using FileManager's ExportToBlob method. This method writes a BLOB (Binary Large Object) into a database from a specified source file. The syntax is:

objectFileManager.ExportToBlob ([in]Source, [in] recordset("column-name"))

The object is saved into the field specified in the column-name argument.
The recordset argument must specify a valid ADO recordset.

Here is an example:

<%
'--- Instantiate FileUp.
'---
Set upl = Server.CreateObject("SoftArtisans.FileUp")
'---
'--- Set the location for the temporary cache of the upload.
'--- This property must be set before referencing any form elements.
'--- In this example, we are setting it to the final destination of the
'--- upload.
upl.path = "c:\UploadDirectory"
'---
'--- Save the file with the user's original filename.
'---
upl.Save
'---
'--- Instantiate FileManager.
'---
set oFM = Server.CreateObject("SoftArtisans.FileManager")
'---
'--- Create an instance of the recordset object.
'---
set rsBlob = Server.CreateObject("adodb.recordset")
'---
'--- Open database connection.
'--- This example uses an Access database with system DNS.
'---
rsBlob.Open "TableName", "DatabaseDNSAlias", 2, 3
'---
'--- For SQL Server Users, use the following line to open the
'--- recordset.
'--- Make sure you replace:
'--- <myserver> with your SQL Server name
'---
'--- rsBlob.Open "TableName", "driver={SQL server};server=<myserver>;uid=sa;pwd=;database=SQLServerUpload", 2, 3
'---
'--- Add new record.
'---
rsBlob.AddNew
'---
'--- Insert the file into the database.
'---
oFM.ExportToBlob upl.ServerName, rsBlob.Fields("filecol")
'---
'--- Update and clean up.
'---
rsBlob.Update
rsBlob.Close
Set rsBlob = Nothing
Set oFM = Nothing
Set upl = Nothing
%>

Created : 1/15/2003 12:00:00 AM (last modified : 1/23/2003 8:52:01 PM)
Rate this article!
 
Comments