<%@language=vbscript%> <% dim Uploader, Path, ConnectionString, Recordset 'Create working directory if it does not exist: Path = Server.MapPath("Temp") MakeDir(Path) 'Modify the connection string to work with other databases (MySQL, MS SQL Server, etc): ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & "\Database.mdb" sub CheckError if Err then 'Report error: Response.Clear Response.Write Err.Source & ": " & Err.Description 'Clean up (it could be more intelligent): on error resume next Recordset.Delete Recordset.Close set Recordset = nothing Stream.Close set Stream = nothing 'Terminate: Response.End end if end sub 'PART 1 '====== 'This part demonstrates how to upload files to database. sub CreateDatabase dim DB, Table const adVarWChar = 202, adDate = 7, adLongVarWChar = 203, adLongVarBinary = 205 on error resume next set DB = Server.CreateObject("ADOX.Catalog") DB.Create ConnectionString set Table = Server.CreateObject("ADOX.Table") Table.Name = "Uploads" Table.Columns.Append "User", adVarWChar 'Username Table.Columns.Append "Date", adDate 'Upload date Table.Columns.Append "Note", adLongVarWChar 'Notes from user Table.Columns.Append "Name", adVarWChar 'Filename Table.Columns.Append "File", adLongVarBinary 'File data DB.Tables.Append Table DB.ActiveConnection.Close set Table = nothing set DB = nothing end sub 'Enable error handling: on error resume next 'Create database if it does not exist and open recordset: set Recordset = Server.CreateObject("ADODB.Recordset") Recordset.LockType = 3 'const adLockOptimistic = 3 Recordset.Open "Uploads", ConnectionString if Hex(Err) = "80004005" then 'Error: Could not find file Data.mdb Err.Clear CreateDatabase CheckError Recordset.Open "Uploads", ConnectionString end if CheckError 'Create new record for current upload: Recordset.AddNew CheckError 'Get reference to ASPUploader: set Uploader = GetASPUploader 'Set Destination to binary (blob) field: set Uploader.Destination = Recordset("File") 'Set longer timeout for large files if needed: Server.ScriptTimeout = 900 'Start receiving and saving file(s): Uploader.Upload CheckError 'Note: In this simplified example, if there were multiple files in the html form, all the files 'would be saved one-by-one in single database field specified as the Destination. However, you 'can save each file to its own database field. See Example 3 to learn how to set different 'destinations for individual files. 'Check if user has uploaded any files at all: if Uploader.Files.Count = 0 then Recordset.Cancel Recordset.Close set Recordset = nothing Response.Write "Please upload a file to test this example." Response.End end if 'Set fields: Recordset("User") = Uploader.Form("User") 'Name of user Recordset("Note") = Uploader.Form("Note") 'Notes from user Recordset("Date") = Now 'Date of upload Recordset("Name") = Uploader.Files("File1").Name 'Name of file 'Update the record: Recordset.Update CheckError 'Clean up: Recordset.Close set Recordset = nothing 'PART 2 '====== 'This part demonstrates how to retrieve the files from the database. dim Stream 'Create and open Recordset: set Recordset = Server.CreateObject("ADODB.Recordset") Recordset.Open "Uploads", ConnectionString 'To simplify the example, we always read data from the first record of the recordset: Recordset.MoveFirst 'Create and open Stream: set Stream = Server.CreateObject("ADODB.Stream") Stream.Open Stream.Type = 1 'const adTypeBinary = 1 'Write data from binary field to Stream: Stream.Write Recordset("File").Value 'You can use GetChunk method (in loop) instead of Value property when working with very large files. 'Save the Stream to file: Path = Path & "\" & Recordset("Name") 'Filename Stream.SaveToFile Path, 2 'adSaveCreateOverWrite = 2 CheckError 'Clean up: Recordset.Close set Recordset = nothing Stream.Close set Stream = nothing 'PART 3 '====== 'This part demonstrates how to send files to user browser for download. 'Create and open Recordset: set Recordset = Server.CreateObject("ADODB.Recordset") Recordset.LockType = 3 'const adLockOptimistic = 3 Recordset.Open "Uploads", ConnectionString 'To simplify the example, we always read data from the first record of the recordset: Recordset.MoveFirst 'Notify the browser it is going to receive a binary file: Response.ContentType = "application/octet-stream" Response.AddHeader "Content-Disposition", "attachment; filename=" & Recordset("Name") Response.AddHeader "Content-Length", Recordset("File").ActualSize 'Write binary data into Response: Response.BinaryWrite Recordset("File").Value 'Note: The above line reads the entire file (Value) before writing it to Response. 'When working with large files, it is recommended to read the file in loop in smaller 'chunks rather than using the Value property. If your file size may be greater than 50MB 'and your system memory is limited, you should use the following loop with GetChunk method 'instead of the Value property. It works with small files as well: 'dim ChunkSize, BytesRead, FileSize 'ChunkSize = 65536 'BytesRead = 0 'FileSize = Recordset("File").ActualSize 'do while BytesRead < FileSize and Response.IsClientConnected ' if FileSize - BytesRead < ChunkSize then ChunkSize = FileSize - BytesRead ' Response.BinaryWrite Recordset("File").GetChunk(ChunkSize) ' Response.Flush ' BytesRead = BytesRead + ChunkSize 'loop CheckError 'Clean up: Recordset.Delete Recordset.Close set Recordset = nothing %>