Home
What's New
User Manual
1. Introduction
2. Simple Uploads
3. Memory Uploads
4. Database
5. Progress Bar
6. Security
7. Images
8. Unicode
9. Miscellaneous
10. Hosting Issues
Object Reference
Live Demos
Support
XUpload
JUpload
AspJpeg
Download
Purchase
Clients
Other Products
Contact Us
|
Chapter 3. Uploading to Memory
Calling Upload.Save with no Arguments
The Path parameter to the method Upload.Save
is optional: the method can be called with no arguments at all, as follows:
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save
If a folder path is not passed in, the Save method does not save
uploaded files to disk. Instead it creates memory images of the files
that can be accessed via the Upload.Files collection. A memory file
can then be saved to disk via the File.SaveAs
method. You may also use the property File.Binary
to save the file in the database, as shown in the next chapter.
Note that AspUpload 2.x used the method Upload.SaveToMemory
to perform memory uploads. This method is still supported for backwards
compatibility, but it is equivalent to Upload.Save with the Path argument omitted.
Why Upload to Memory
Memory uploads provide several major advantages over regular uploads:
- Access to form items. You cannot access
text fields on your form until after the Save method is called. Therefore, if you need to access a form item
before saving a file to disk, uploading to memory is the only way to do it.
- Access to file names. You can check
whether a file with the same name already exists in a directory
before saving the uploaded file to disk.
- Simplified renaming and copying. A regular upload saves all the files
in the same directory, and under their original names. Uploading to memory
makes the task or renaming and copying files easier to code.
- Performance and security. If the ultimate destination
of an uploaded file is a database table, you can use memory uploads
to save your file in the database directly from memory without saving it to disk first.
The main disadvantage of memory uploads is that it cannot be used with
large files as it is very memory-intensive. You should always use
SetMaxSize with memory uploads to limit the size of the memory imprints of files.
Accessing Form Items
The following code sample enables a user to select a destination
subdirectory underneath the c:\upload folder to upload a file to.
The sample form (file formitems.asp) contains a file item
and a drop-down list box:
<HTML>
<BODY BGCOLOR="#FFFFFF">
<h3>Using memory uploads to access form items</h3>
<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="formitems_upload.asp">
Select file: <INPUT TYPE="FILE" SIZE="40" NAME="FILE1"><BR>
Select destination subdirectory:
<SELECT NAME="Subdir">
<OPTION>Folder1</OPTION>
<OPTION>Folder2</OPTION>
<OPTION>Folder3</OPTION>
</SELECT>
<BR>
<INPUT TYPE=SUBMIT VALUE="Upload!">
</FORM>
</BODY>
</HTML>
|
The corresponding upload script formitems_upload.asp
uploads the file to memory, then creates a user-specified subfolder
and saves the file in it:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' we use memory uploads, so we must limit file size
Upload.SetMaxSize 100000, True
' Save to memory. Path parameter is omitted
Upload.Save
' Access subdirectory specified by user
subdir = Upload.Form("subdir")
' Build path string
Path = "c:\upload\" & subdir
' Create path, ignore "already exists" error
Upload.CreateDirectory Path, True
' Save files to it. Our form has only one file item
' but this code is generic.
For Each File in Upload.Files
File.SaveAs Path & "\" & File.FileName
Response.Write "File saved as " & File.Path & "<BR>"
Next
%>
</BODY>
</HTML>
|
Click the link below to run this code sample:
http://localhost/aspupload/03_memory/formitems.asp
Accessing File Names
The next code sample uploads a file to memory, the checks whether
a file with the same name already exists in the destination directory,
and if so, alerts a user. Otherwise the file is saved to disk.
The form file filename.asp contains a single file item and is not shown here.
The corresponding upload script filename_upload.asp uses the method
FileExists to determine whether the file already exists:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' we use memory uploads, so we must limit file size
Upload.SetMaxSize 100000, True
' Save to memory. Path parameter is omitted
Upload.Save
' Check whether a file was selected
Set File = Upload.Files("FILE1")
If Not File Is Nothing Then
' Obtain file name
Filename = file.Filename
' check if file exists in c:\upload under this name
If Upload.FileExists("c:\upload\" & filename ) Then
Response.Write "File with this name already exists."
Else
' otherwise save file
File.SaveAs "c:\upload\" & File.Filename
Response.Write "File saved as " & File.Path
End If
Else ' file not selected
Response.Write "File not selected."
End If
%>
</BODY>
</HTML>
|
Click the link below to run this code sample:
http://localhost/aspupload/03_memory/filename.asp
Uploading Files to Two Folders
Using regular uploads, all the files uploaded at once are saved in the same directory,
and under their original names. Memory uploads enable you to save
files individually in any directory and under arbitrary names.
The next code sample enables a user to upload two files. One file
is saved in the directory "c:\upload\folder1", the other
in "c:\upload\folder2", both under the name derived from the current session ID.
The file twofolders.asp contains a form with two file items, FILE1 and FILE2.
This file is not shown here. The corresponding upload script twofolders_upload.asp
creates two subdirectories and saves FILE1 in Folder1 and FILE2 in Folder2. Both
files are saved under the same name derived from SessionID and the original extension:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' we use memory uploads, so we must limit file size
Upload.SetMaxSize 100000, True
' Save to memory. Path parameter is omitted
Count = Upload.Save
' Two files must be selected
If Count <> 2 Then
Response.Write "You must select 2 files."
Response.End
End If
' Create two folders, ignore "already exists" error
Upload.CreateDirectory "c:\upload\Folder1", True
Upload.CreateDirectory "c:\upload\Folder2", True
' Obtain File objects
Set File1 = Upload.Files("FILE1")
Set File2 = Upload.Files("FILE2")
' Build name from session ID
Name = Session.SessionID
' Save
File1.SaveAs "c:\upload\Folder1\" & Name & File1.Ext
Response.Write "File 1 is saved under " & File1.Path & "<BR>"
File2.SaveAs "c:\upload\Folder2\" & Name & File2.Ext
Response.Write "File 2 is saved under " & File2.Path & "<BR>
%>
</BODY>
</HTML>
|
Click the link below to run this code sample:
http://localhost/aspupload/03_memory/twofolders.asp
Using uploads to memory to save files in the database is covered in the next chapter.
Uploading Files to Two Folders (Another Approach)
To copy and/or rename files, you don't have to use memory uploading.
The same effect can be achieved using regular (disk) uploading in conjunction
with the methods File.Copy, File.Move and File.Delete,
which copy, move and delete an uploaded file, respectively.
The code sample twofoders2_upload.asp does essentially the same
as twofolders_upload described above, but without the use of memory uploading:
<HTML>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload")
' Save uploaded files to a temporary folder
Count = Upload.Save("c:\upload")
' Two files must be selected
If Count <> 2 Then
Response.Write "You must select 2 files."
' delete uploaded file, if any
For Each File in Upload.Files
File.Delete
Next
Response.End
End If
' Create two folders, ignore "already exists" error
Upload.CreateDirectory "c:\upload\Folder1", True
Upload.CreateDirectory "c:\upload\Folder2", True
' Obtain File objects
Set File1 = Upload.Files("FILE1")
Set File2 = Upload.Files("FILE2")
' Build name from session ID
Name = Session.SessionID
' Copy file 1 to folder 1
File1.Copy "c:\upload\Folder1\" & Name & File1.Ext
' Delete from temp folder
File1.Delete
Response.Write "File 1 is copied to Folder 1<BR>"
' Copy file 2 to folder 2
File2.Copy "c:\upload\Folder2\" & Name & File2.Ext
' Delete from temp folder
File2.Delete
Response.Write "File 2 is copied to Folder 2<BR>"
%>
</BODY>
</HTML>
|
Click the link below to run this code sample:
http://localhost/aspupload/03_memory/twofolders2.asp
Copyright © 1998 - 2001 Persits Software, Inc.
All Rights Reserved
AspUpload® is a registered trademark of Persits Software, Inc.
Questions? Comments? Write us!
|