Class

WebFileUploader


Description

WebFileUploader allows the user to choose files to be uploaded. You can then upload the files by calling the StartUpload method.

Methods

Name

Parameters

Returns

Shared

CancelUpload

Close

ExecuteJavaScript

script As String

GotoURL

URL As String, inNewWindow As Boolean = False

RemoveAllFiles

StartUpload

UpdateBrowser

Events

Name

Parameters

Returns

Closed

ContextualMenuSelected

hitItem As WebMenuItem

FileAdded

fileName As String, bytes As UInt64, MIMEType As String

FileRemoved

fileName As String

Hidden

Opening

Shown

UploadAborted

UploadError

error As RuntimeException

UploadFinished

files() As WebUploadedFile

UploadProgressed

percent As Integer

UploadStarted

fileCount As Integer

Property descriptions


WebFileUploader.Caption

Caption As String

The caption for the button the user clicks to select a file. The default value is Select.


WebFileUploader.ContextualMenu

ContextualMenu As WebMenuItem

If you assign a WebMenuItem to the control, it will be displayed when the user right-clicks the control.

On a WebPage, you can disable/remove the default contextual menu by an empty WebMenuItem class object to this property.

This code populates a contextual menu in the Shown event of the control.

Var menu As New WebMenuItem

menu.AddMenuItem("One")
menu.AddMenuItem("Two")
menu.AddMenuItem("Three")
Me.ContextualMenu = menu

The menu selection is then handled by the ContextualMenuSelected event when the user right-clicks on the control. For example, it can be of the form:

Select case hitItem.Value
Case "One"
  MessageBox("One")
Case "Two"
  MessageBox("Two")
Case "Three"
  MessageBox("Three")
End Select

WebFileUploader.ControlID

ControlID As String

Identifies the control on a per session basis.

This property is read-only.


WebFileUploader.Enabled

Enabled As Boolean

When True the WebControl is drawn enabled and responds to user action. When False, the control appears as disabled and does not respond to user actions.

In the case of WebTimer, when set to False this disables and stops the WebTimer. When set to True, it starts the WebTimer.

Disable a button when a check box value changes:

If AllowSaveCheckBox.Value Then
  SaveButton.Enabled = True
Else
  AllowSaveButton.Enabled = False
End If

WebFileUploader.Filter

Filter As String

The MIME type of files that an be chosen for upload.

Separate multiple MIME types with a semicolon.

This code in the Shown event handler for the WebFileUploader lets you choose PNG and JPG files:

Me.Filter = "image/png;image/jpeg"

WebFileUploader.MaximumBytes

MaximumBytes As Integer

The maximum number of bytes that can be uploaded, across all files selected, by a single call to StartUpload.


WebFileUploader.MaximumFileCount

MaximumFileCount As Integer

The maximum number of files that can be uploaded.


WebFileUploader.Name

Name As String

The name of the control.

This property is read-only.


WebFileUploader.Page

Page As WebPage

Identifies the web page that contains the control.

This property is read-only.


WebFileUploader.Parent

Parent As WebControl

Used to get the control's parent control or page. If the parent control is a WebContainer, then it returns the WebContainer. If it is on a WebPage, it returns the WebPage.

This property is read-only.


WebFileUploader.UploadTimeout

UploadTimeout As Integer

The number of seconds that, once passed, will cause the UploadError event to be called.

The default is 0, which means "no timeout". Be aware that not having a timeout can result in out-of-memory issues for some web apps when uploading large (> 32MB) files.

Method descriptions


WebFileUploader.CancelUpload

CancelUpload

Cancels the upload.


WebFileUploader.Close

Close

Removes the control from the page.


WebFileUploader.ExecuteJavaScript

ExecuteJavaScript(script As String)

Executes the JavaScript passed. The JavaScript passed can call a JavaScript function in a WebPageSource control.

The Xojo web framework uses EcmaScript 6 which is more strict than previous versions of JavaScript. For more details, see the EcmaScript 6 documentation.

This code in the Pressed event of a Button displays an alert using JavaScript:

Me.ExecuteJavaScript("alert('Hello!');")

This code will select the text in a WebTextField (or WebTextArea):

WebTextField1.ExecuteJavascript("document.getElementById('" + _
  WebTextField1.ControlID + "_inner').select();")

WebFileUploader.GotoURL

GotoURL(URL As String, inNewWindow As Boolean = False)

Opens the passed URL in place of the current web page or downloads a file. If InNewWindow is True, the browser is asked to open the URL in a new window.

If the browser has popup windows disabled and InNewWindow is True, the method silently fails and the page is not shown.

If InNewWindow is False, the running web app is replaced with the specified URL. If you want to display an external web site within your web app, use the WebHTMLViewer control.

Display a web site in a new popup window:

Me.GotoURL("http://www.wikipedia.org", True)

WebFileUploader.RemoveAllFiles

RemoveAllFiles

Removes all files from the upload queue.

If an upload is already in progress, any files that had been added before the upload began will still be uploaded even if RemoveAllRows is called. This method is useful in cases where after an initial upload has begun and the user wants to prepare for another upload.


WebFileUploader.StartUpload

StartUpload

Starts the upload process.

If you call this method when there are no files to upload or while a previous upload is still in progress, the UploadError event will fire.


WebFileUploader.UpdateBrowser

UpdateBrowser

Forces the current values of the control to be sent to the browser.

This method is useful when you are computing values in a loop and wish to update the browser immediately rather than wait until the current method ends.

This code iterates through a RowSet of database rows, updates a ProgressBar and then forces the updated ProgressBar to be sent to the browser via UpdateBrowser.

ProgressBar1.Maximum = SalesData.RowCount
For Each row As DatabaseRow in SalesData
 AnalyzeSales(row)
 ProgressBar1.Value = ProgressBar1.Value + 1
 ProgressBar1.UpdateBrowser
Next

Event descriptions


WebFileUploader.Closed

Closed

The control has been removed from the browser either because the page has closed or the control's Close method was called.


WebFileUploader.ContextualMenuSelected

ContextualMenuSelected(hitItem As WebMenuItem)

Called when a contextual menu item is selected. This selected item is contained in hitItem.

This code populates a contextual menu in the Opening event of a WebToolbar:

Var menu As New WebMenuItem

menu.AddMenuItem("One")
menu.AddMenuItem("Two")
menu.AddMenuItem("Three")
Me.ContextualMenu = menu

The menu selection is then handled by the ContextualMenuSelected event when the user right-clicks on the control. For example, it can be of the form:

Select case hitItem.Value
Case "One"
  MessageBox("One")
Case "Two"
  MessageBox("Two")
Case "Three"
  MessageBox("Three")
End Select

WebFileUploader.FileAdded

FileAdded(fileName As String, bytes As UInt64, MIMEType As String)

The file called Name has been added.


WebFileUploader.FileRemoved

FileRemoved(fileName As String)

The file passed has been removed from the upload queue.


WebFileUploader.Opening

Opening

The control has been created and the page is opening but has not been sent to the browser yet.

The Opening event handler can be used to initialize non-visual properties and settings for controls.

In most cases, you should use the Shown event to initialize controls.


WebFileUploader.Shown

Shown

The control has appeared on the currently displayed page. This could be because its parent page just finished loading, its parent page has come to the foreground or the control is now visible having been previously invisible because it or its parent control's Visible property has been set to True.

Use the Shown event for initializing your controls or doing anything that would interact with other controls or user interface elements on the web page instead of the Opening event.

Note

This event is the web equivalent to the DesktopWindow.Activated event.


WebFileUploader.Hidden

Hidden

The control is about to become no longer visible. This could be because the page is being closed, is being replaced as the foreground page by another page or because the control or a parent control's Visible property has been set to False.

Note

This event is equivalent to the DesktopWindow.Deactivated event in a desktop app.


WebFileUploader.UploadAborted

UploadAborted

The uploaded was cancelled.

This occurs when, during an upload, the user closes the tab/window, closes the browser, the browser quits or crashes for some reason. Basically, this event occurs when the upload is abruptly aborted for any reason other than the app calling the CancelUpload method.


WebFileUploader.UploadError

UploadError(error As RuntimeException)

Called when an error occurs during the upload process.


WebFileUploader.UploadFinished

UploadFinished(files() As WebUploadedFile)

The upload has finished.

There will be a delay between the time files finish uploading and the UploadFinished event is called because the files must be processed once they are uploaded.


WebFileUploader.UploadProgressed

UploadProgressed(percent As Integer)

The uploaded has progressed to the percentage indicated.

There will be a delay between the time files finish uploading and the UploadFinished event is called because the files must be processed once they are uploaded.


WebFileUploader.UploadStarted

UploadStarted(fileCount As Integer)

The upload has started by the StartUpload method being called.

FileCount is the number of files that have been added to be uploaded.

Notes

There will be a delay between the time files finish uploading and the UploadFinished event is called because the files must be processed once they are uploaded.

WebFileUploader will always try to write files to the temporary folder on the server if possible to save memory. If that is not possible (because the temporary folder doesn't exist or is not writable), the files are copied into a MemoryBlock.

Sample code

To allow files to be uploaded by your web apps, add a FileUploader control to a web page. By itself, the user uses the Select button choose a file to be uploaded. Each time the user chooses a file, that file is added to the upload queue. If you wish to show the user a list of files they have chosen, add a WebListBox to the layout and then add files to that control when the FileAdded event handler is called. You'll likely also want to add a WebButton to the page for the user to click to start the upload. In it's Pressed event handler, call StartUpload:

FileUploader1.StartUpload

After the files have been uploaded, they will be in memory and the UploadFinished event handler is called. Here you can process the uploaded files and choose to do something with them. This code saves uploaded files to disk (you'll need to make sure your web app has the correct permissions on the server in order to write files):

Var outputFile As FolderItem
Var output As BinaryStream
For Each file As WebUploadedFile In files
  Try
  outputFile = New FolderItem(file.Name)
  output = BinaryStream.Create(outputFile, True)
  output.Write(file.Data)
  output.Close
  Catch e As IOException
    Continue
  End Try
Next

Compatibility

Web projects on all supported operating systems.

See also

WebUIControl parent class; WebUploadedFile