UserGuide

Web File Uploader

From Xojo Documentation

The File Uploader control allows users to make a list of files to upload to the web server. File Uploader has its own user interface that cannot be changed. The user can add select one or more files at once. To upload the files, you call the StartUpload method, usually from an accompanying Button.

Uploaded files larger than 256K are written directly to the temporary folder if it is writeable. The file is kept in memory if the temporary folder is not writeable or the file is 256K or smaller. For files kept in memory, be aware that the maximum available RAM you have on your web server will limit the size of files that can be uploaded.

Below is a list of commonly used events, properties and methods. For the complete list, refer to WebFileUploader in the Language Reference.

Events

UploadFinished - Called when the files have finished uploading. The event provides an array of the uploaded files for you to loop through and process.

Properties

MaximumFileCount - Indicates the maximum number of files the user can upload at once.

Methods

StartUpload - Call this method to start uploading the files that were added to the control.

Uploaded File

When the UploadFinished event is called, the parameter contains an array of the uploaded files. You can loop through these files to read them in or so save them to permanent locations.

Below are common properties and methods. Refer to WebUploadedFile in the Language Reference for the complete list.

Properties

Data - The contents of the file.

File - A Folder Item that points to the file, if it was uploaded to the temporary folder.

Name - The name of the uploaded file.

Methods

Save - Use this method to see the uploaded file to a permanent location.

Usage

This code in the Pressed event handler for a Button starts uploading the files that have been added to the File Uploader:

FileUploader1.StartUpload

After the upload has completed, you can process the files in the UploadFinished event handler. This code saves the uploaded files to the Documents folder:

Var bs As BinaryOutputStream
Var f As FolderItem

For Each file As WebUploadedFile In Files
f = SpecialFolder.Documents.Child(file.Name)
Try
bs = BinaryStream.Create(f, True)
bs.Write(file.Data)
bs.Close
Catch e As IOException
// Error, skip file
Continue
End Try
Next

However, it is simpler and uses less memory to just call the Save method of the uploaded file to save it to a permanent location:

Var saveFile As FolderItem
For Each file As WebUploadedFile In Files
saveFile = SpecialFolder.Documents.Child(file.Name)
Try
file.Save(saveFile)
Catch e As IOException
// File Error, skip file
Continue
End Try
Next

Example Project

The WebFileUploader example shows you how to let the user choose files to add and then upload them.

Examples/Web/Controls/WebFileUploader

See Also

WebFileUploader, WebUploadedFile classes; UserGuide:Web Files topic