<%@  language="VBScript" %>
<!--#INCLUDE FILE="../Config.asp"-->
<%
Server.ScriptTimeout = 450

'This variable specifies relative path to the folder, where the gallery with uploaded files is located.
'Do not forget about the slash in the end of the folder name.
Dim strGalleryPath
strGalleryPath = "../UploadedFiles/"

Sub ProcessABCUpload
	'Create ABCUpload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("ABCUpload4.XForm")
	objUpload.MaxUploadSize = 20971520
	objUpload.AbsolutePath = True

	' Check that all files are uploaded
	If objUpload.Form("RequestComplete").Count = 0 Then
		Exit Sub
	End If

	'Clear files and data uploaded at previous time.
	If CInt(objUpload.Form("PackageIndex")(1)) = 0 Then

		Dim objFolder, objFile

		'Delete source files.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath))
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

	End If

	'NOTE: If you do not want to delete previously uploaded files, just 
	'remove or comment out the code above.

	'Create XML file which will keep information about files (image dimensions, description, etc). 
	'XML is used solely for brevity. In real-life application most likely you will use database instead.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument")
	objDescriptions.load Server.MapPath(strGalleryPath & "files.xml")
	If objDescriptions.documentElement Is Nothing Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	End If

	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(objUpload.Form("PackageFileCount")(1))

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 0 To intFileCount - 1
		'Get source file and save it to disk.
		Dim objSourceFile
		Set objSourceFile = objUpload.Files("File0_" & i)
		Dim strFileName
		strFileName = GetSafeFileName(objSourceFile.SafeFileName)		
		objSourceFile.Save Server.MapPath(strGalleryPath & strFileName)		

		'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("File1_" & i)
		objThumbnail1File.Save Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")

		'Save file info.
		Dim objXmlFile	
		Set objXmlFile = objDescriptions.createElement("file")
		objXmlFile.setAttribute "name", strFileName
		objXmlFile.setAttribute "source", strFileName
		objXmlFile.setAttribute "thumbnail", strFileName & ".jpg"

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	objDescriptions.save Server.MapPath(strGalleryPath & "files.xml")
End Sub

Sub ProcessDundasUpload
	'Create Dundas Upload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("Dundas.Upload.2")
	objUpload.UseVirtualDir = False
	objUpload.MaxUploadSize = 1000000000
	
	'Fetch first source file to populate Form collection.
	Dim objSourceFile
	Set objSourceFile = objUpload.GetNextFile

	'Clear files and data uploaded at previous time.
	If CInt(objUpload.Form("PackageIndex").Value) = 0 Then

		Dim objFolder, objFile

		'Delete source files.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath))
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

	End If

	'NOTE: If you do not want to delete previously uploaded files, just 
	'remove or comment out the code above.

	'Create XML file which will keep information about files (image dimensions, description, etc). 
	'XML is used solely for brevity. In real-life application most likely you will use database instead.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument")
	objDescriptions.load Server.MapPath(strGalleryPath & "files.xml")
	If objDescriptions.documentElement Is Nothing Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	End If

	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(objUpload.Form("PackageFileCount").Value)

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 0 To intFileCount - 1
		'Get source file and save it to disk.
		If i > 0 Then
			Set objSourceFile = objUpload.GetNextFile
		End If		
		
		'Get source file and save it to disk.
		Dim strFileName
		strFileName = GetSafeFileName(objUpload.GetFileName(objSourceFile.OriginalPath))
		objSourceFile.Save Server.MapPath(strGalleryPath)
		objUpload.FileMove Server.MapPath(strGalleryPath & objSourceFile.FileName), _
			Server.MapPath(strGalleryPath & strFileName)		

		'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.GetNextFile		
		objThumbnail1File.Save Server.MapPath(strGalleryPath)
		objUpload.FileMove Server.MapPath(strGalleryPath & objThumbnail1File.FileName), _
			Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")

		'Save file info.
		Dim objXmlFile	
		Set objXmlFile = objDescriptions.createElement("file")
		objXmlFile.setAttribute "name", strFileName
		objXmlFile.setAttribute "source", strFileName
		objXmlFile.setAttribute "thumbnail", strFileName & ".jpg"

		objDescriptions.documentElement.appendChild objXmlFile

	Next

	objDescriptions.save Server.MapPath(strGalleryPath & "files.xml")
End Sub

Sub ProcessPersitsAspUpload
	'Create Persits Upload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("Persits.Upload")
	objUpload.CodePage = 65001 'UTF8
	objUpload.Save

	' Check that all files are uploaded
	If objUpload.Form("RequestComplete").Value = "" Then
		Exit Sub
	End If

	'Clear files and data uploaded at previous time.
	If CInt(objUpload.Form("PackageIndex").Value) = 0 Then

		Dim objFolder, objFile

		'Delete source files.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath))
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

	End If

	'NOTE: If you do not want to delete previously uploaded files, just 
	'remove or comment out the code above.

	'Create XML file which will keep information about files (image dimensions, description, etc). 
	'XML is used solely for brevity. In real-life application most likely you will use database instead.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument")
	objDescriptions.load Server.MapPath(strGalleryPath & "files.xml")
	If objDescriptions.documentElement Is Nothing Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	End If
	
	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(objUpload.Form("PackageFileCount").Value)

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 0 To intFileCount - 1
		
		'Get source file and save it to disk.
		Dim objSourceFile, strFileName
		Set objSourceFile = objUpload.Files("File0_" & i)
		strFileName = GetSafeFileName(objSourceFile.FileName)
		objSourceFile.SaveAs Server.MapPath(strGalleryPath & strFileName)		

		'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("File1_" & i)
		objThumbnail1File.SaveAs Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")

		'Save file info.
		Dim objXmlFile	
		Set objXmlFile = objDescriptions.createElement("file")
		objXmlFile.setAttribute "name", strFileName
		objXmlFile.setAttribute "source", strFileName
		objXmlFile.setAttribute "thumbnail", strFileName & ".jpg"

		objDescriptions.documentElement.appendChild objXmlFile

	Next

	objDescriptions.save Server.MapPath(strGalleryPath & "files.xml")
End Sub

'This method verifies whether file with such name already exists 
'and if so, construct safe filename name (to avoid collision).	
Function GetSafeFileName(strFileName)	
	Dim strNewFileName
	strNewFileName = strFileName
	Dim j 
	j = 1
	While (objFileSystem.FileExists(Server.MapPath(strGalleryPath & strNewFileName)))
		strNewFileName = j & "_" & strFileName
		j = j + 1
	Wend
	GetSafeFileName = strNewFileName
End Function

Sub ProcessUpload
	Select Case strUploadComponent
		Case "ABCUpload"
			ProcessABCUpload
		Case "DundasUpload"
			ProcessDundasUpload
		Case "PersitsAspUpload"
			ProcessPersitsAspUpload			
	End Select		
End Sub

Dim objFileSystem
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")

If Request.ServerVariables("REQUEST_METHOD")= "POST" Then
	ProcessUpload
End If
%>