WebFile

From Xojo Documentation

Class (inherits from Object)

WebFile is used to create binary content for the user to download. As long as the WebFile is in memory (something has a reference to it) the file will be available. Once the object is disposed of, the file will no longer be available for download. Make sure you keep a reference in a WebPage, the session or on a control to make sure the WebFile lives long enough for the download to finish.


Events
Downloaded


Properties
Cached fa-lock-32.png ForceDownload Session
Data Identifier fa-lock-32.png URL fa-lock-32.png
FileName MIMEType UseCompression


Methods
Download


Shared Methods
Open

Notes

To get a reference to an existing FolderItem, use the Open shared method.

Example

This example creates a text file to make available for download. You can put it on the Action event handler for a WebButton so that the file is downloaded when the user clicks the button:

TextFile = New WebFile // "TextFile As WebFile" is a property of the web page
TextFile.MimeType = "text/plain"
TextFile.ForceDownload = True // If False, the browser may try to display the file instead of download it
TextFile.FileName = "TextFile.txt"

TextFile.Data = "Hello, world!"

ShowURL(TextFile.URL) // This causes the file to be downloaded

If you instead want to download an existing file on the server, you need to create a WebFile that points to it and save the reference. The best way to do this is to use a property of the WebApplication class. In the App.Open event handler:

Var f As FolderItem = New FolderItem("MyFile.txt")
If f <> Nil And f.Exists Then
App.MyFile = WebFile.Open(f) // "MyFile As WebFile" is a property on the App object
App.MyFile.ForceDownload = True
End If

Now in a WebButton on a page, you can download this file like this:

If App.MyFile <> Nil Then
ShowURL(App.MyFile.URL) // Download the file
End If

See Also

HTMLViewer, WebImageView, WebPicture, FolderItem, ShowURL