The following steps described how a file is being transferred to the server:
- Stage 1: File is transferred from client side to TempFileLocation;
- User browses a file on the client side;
- If the uploader's
AutoUpload
is set to true, the upload would automatically starts;
-
When AutoUpload is false (default value), the user must
click the Upload to start uploading;
-
The uploader transfers the file to TempFileLocation.
A number of files are created for each file to be uploaded. All these
files have long temporary file names that are not in anyway related
to the original file name of the file being uploaded. You do not need
to clean up these files;
- Stage 2: User submits the page;
-
Even after the file has been completely transferred to TempFileLocation,
they do not automatically become available to your code. In order for them to
become available to your code, user must submit the page. Think the uploader control
as a textbox and "uploading a file" as "type a letter in the textbox". The textbox
now have contents, but user must submit the page (usually by another button control)
in order for server side code to access the textbox contents.
The uploader control can also automatically submit the page as soon as the upload
is done. To use this feature, set the uploader's
AutoPostBack to true;
- Once the page is submitted, the uploader's
FileUploaded event is fired.
Inside this event you can use
PostedFiles
to get information
about each posted files. Each posted file is represented by an
AJAXPostedFile object. Most importantly,
the object contains the file's original name and the temporary file name. At this
stage, you can copy the file to any destination you wish, usually also rename it to
back to its originally file name:
[C#]
foreach (EO.Web.AJAXPostedFile file in AJAXUploader1.PostedFiles)
{
//Get the temp file name
string tempFileName = file.TempFileName;
//Create the final file name based on the original file name
string finalFileName = System.IO.Path.Combine("c:\upload\", file.ClientFileName);
//Move the file to the desired location
System.IO.File.Move(tempFileName, finalFileName);
}
[Visual Basic.NET]
Dim file As EO.Web.AJAXPostedFile
For Each file In AJAXUploader1.PostedFiles
'Get the temp file name
Dim tempFileName As String = file.TempFileName;
'Create the final file name based on the original file name
Dim finalFileName As String = System.IO.Path.Combine("c:\upload\", file.ClientFileName)
'Move the file to the desired location
System.IO.File.Move(tempFileName, finalFileName)
Next file
The above code may not be necessary if
FinalFileLocation is set.
-
Stage 3: moving files from TempFileLocation to FinalFileLocation.
You can optionally set the uploader's FinalFileLocation
property. When this property is set, the uploader automatically
moves the files from TempFileLocation to FinalFileLocation when
the page submits. In this case the above sample code that moves
posted files is no longer necessary;
Even though FinalFileLocation is convenient, it may not
always the best option. For example, if you store files
in your database, there is no need to set FinalFileLocation
to introduce an extra copy of the file.
Instead you should read directly from files in TempFileLocation,
because all the information for you to create the database
record is already available at that stage;
- Stage 4: Clean up
The uploader automatically cleans up files in TempFileLocation
after it detects the files have not been accessed after a certain
period of time. You do not need to write any code to clean up.
Sometimes you may notice the files are not deleted on your development
machine after a long time. This usually occurs when:
- You included TempFileLocation in your project. Visual
Studio scans files included in the project, which marks the files
as being recently used. This prevents the uploader from deleting
the files;
- The uploader does not delete the files immediately. So if the
application has never run long enough to satisfy the time delay
(which is often the case on a development machine), the files
may not get deleted;
- You have changed TempFileLocation. When TempFileLocation
is changed, the previous temporary file directory will no longer
be monitored. Thus files in that directory will not be deleted;
In any case the files should not be a concern in a properly configured
production environment.