Class

# WebFile

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

<span class="title-ref">WebFile</span> is used to create binary content for the user to download. As long as the <span class="title-ref">WebFile</span> 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</api/user_interface/web/webpage>`, the session or on a control to make sure the <span class="title-ref">WebFile</span> lives long enough for the download to finish.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                     | Type                               | Read-Only | Shared |
|------------------------------------------|------------------------------------|-----------|--------|
| `Cached<webfile.cached>`                 | `Boolean</api/data_types/boolean>` | ✓         |        |
| `FileName<webfile.filename>`             | `String</api/data_types/string>`   |           |        |
| `ForceDownload<webfile.forcedownload>`   | `Boolean</api/data_types/boolean>` |           |        |
| `MIMEType<webfile.mimetype>`             | `String</api/data_types/string>`   |           |        |
| `Session<webfile.session>`               | `WebSession</api/web/websession>`  |           |        |
| `URL<webfile.url>`                       | `String</api/data_types/string>`   | ✓         |        |
| `UseCompression<webfile.usecompression>` | `Boolean</api/data_types/boolean>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                             | Parameters                                                                                                               | Returns                                  | Shared |
|----------------------------------|--------------------------------------------------------------------------------------------------------------------------|------------------------------------------|--------|
| `Data<webfile.data>`             |                                                                                                                          | `MemoryBlock</api/language/memoryblock>` |        |
|                                  | `Assigns</api/language/assigns>` value As `MemoryBlock</api/language/memoryblock>`                                       |                                          |        |
| `Download<webfile.download>`     |                                                                                                                          | `Boolean</api/data_types/boolean>`       |        |
| `Identifier<webfile.identifier>` |                                                                                                                          | `String</api/data_types/string>`         |        |
| `Open<webfile.open>`             | file As `FolderItem</api/files/folderitem>`, inMemory As `Boolean</api/data_types/boolean>` = `True</api/language/true>` | <span class="title-ref">WebFile</span>   | ✓      |

## Events

<div class="rst-class">

table-centered_column_4

</div>

| Name                             | Parameters | Returns |
|----------------------------------|------------|---------|
| `Downloaded<webfile.downloaded>` |            |         |

## Property descriptions

<div id="webfile.cached">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Cached

**Cached** As `Boolean</api/data_types/boolean>`

> Indicates whether or not the file is cached on the browser.
>
> This property is read-only.

<div id="webfile.filename">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.FileName

**FileName** As `String</api/data_types/string>`

> The name of the file.
>
> Create a new file for downloading:
>
> ``` xojo
> TextFile = New WebFile ' TextFile 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!"
>
> Self.GoToURL(TextFile.URL) ' This causes the file to be downloaded
> ```

<div id="webfile.forcedownload">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.ForceDownload

**ForceDownload** As `Boolean</api/data_types/boolean>`

> Indicates whether the browser will always download the file instead of trying to display it (depending on the `MIMEType<webfile.mimetype>`). The default is `False</api/language/false>`.
>
> When `True</api/language/true>`, the browser will always download the file rather than try to display it. For example, most web browsers will try to display simple text files and many will display PDF files inline. Set ForceDownload to `True</api/language/true>` to instead ensure these types of files are downloaded instead.
>
> If you find that FireFox users can't download files, try using `Self.GoToURL<webcontrol.gotourl>` and pass `True</api/language/true>` for the InNewWindow parameter as well as having the user disable popup blockers.
>
> Create a new file for downloading:
>
> ``` xojo
> TextFile = New WebFile ' TextFile 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!"
>
> Self.GoToURL(TextFile.URL) ' This causes the file to be downloaded
> ```

<div id="webfile.mimetype">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.MIMEType

**MIMEType** As `String</api/data_types/string>`

> This is the MIME type of the content. Browsers rely on this to determine what to do with the content. Setting `WebFile<webfile.forcedownload>` to `True</api/language/true>` will override the MIMEType. Defaults to "application/octet-stream".
>
> This example creates a text file to make available for download. You can put it on the Pressed event handler for a `WebButton</api/user_interface/web/webbutton>` so that the file is downloaded when the user clicks the button:
>
> ``` xojo
> TextFile = New WebFile ' TextFile 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!"
>
> Self.GoToURL(TextFile.URL) ' This causes the file to be downloaded
> ```

<div id="webfile.session">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Session

**Session** As `WebSession</api/web/websession>`

> Session defaults to the current session which means it will only be accessible from that session.
>
> <div class="tip">
>
> <div class="title">
>
> Tip
>
> </div>
>
> Maintaining separate files for each session will use up more memory. To make a file accessible to all sessions and thus be stored in memory only once, set the Session property to `Nil</api/language/nil>`.
>
> </div>

<div id="webfile.url">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.URL

**URL** As `String</api/data_types/string>`

> The url to the file. Best used with `WebControl.GoToURL<webcontrol.gotourl>`.
>
> This property is read-only.
>
> The Downloading example project uses <span class="title-ref">WebFile</span>.URL to download the png test file that was opened in the `WebApplication</api/web/webapplication>` event. The code for the Static Download button is:
>
> ``` xojo
> If App.LogoFile <> Nil Then
>   Self.GoToURL(App.LogoFile.URL)
> Else
>   MessageBox("Whoops, no logo file available")
> End If
> ```

<div id="webfile.usecompression">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.UseCompression

**UseCompression** As `Boolean</api/data_types/boolean>`

> Determines if the web framework should compress a file before sending it to browsers that support gzip compression. The default value is `True</api/language/true>`.

## Method descriptions

<div id="webfile.data">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Data

**Data** As `MemoryBlock</api/language/memoryblock>`

**Data**(`Assigns</api/language/assigns>` value As `MemoryBlock</api/language/memoryblock>`)

> Contains the data in the file. *Data* is initially a 0-byte `MemoryBlock</api/language/memoryblock>`. The developer should create a new `MemoryBlock</api/language/memoryblock>` of the size that will be needed to store the data.
>
> You can also just assign a `String</api/data_types/string>` as the *Data*.
>
> Create a new file for downloading:
>
> ``` xojo
> TextFile = New WebFile ' TextFile 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!"
>
> Self.GoToURL(TextFile.URL) ' This causes the file to be downloaded
> ```

<div id="webfile.download">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Download

**Download** As `Boolean</api/data_types/boolean>`

> Asks the browser to download the file. If this method returns `True</api/language/true>`, it means that the request was sent to the browser, not that it was actually downloaded.
>
> This example creates a text file to make available for download. You can put it on the Pressed event handler for a `WebButton</api/user_interface/web/webbutton>` so that the file is downloaded when the user clicks the button:
>
> ``` xojo
> TextFile = New WebFile ' TextFile 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!"
>
> If TextFile.Download Then ' This causes the file to be downloaded
>   ' Download requested
> End If
> ```

<div id="webfile.identifier">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Identifier

**Identifier** As `String</api/data_types/string>`

> An identifier that is unique to this <span class="title-ref">WebFile</span>.

<div id="webfile.open">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Open

**Open**(file As `FolderItem</api/files/folderitem>`, inMemory As `Boolean</api/data_types/boolean>` = `True</api/language/true>`) As <span class="title-ref">WebFile</span>

> Converts the passed `FolderItem</api/files/folderitem>` to a <span class="title-ref">WebFile</span> that can be downloaded. The entire *file* is loaded in memory when *inMemory* is `True</api/language/true>` (the default). When *inMemory* is `False</api/language/false>`, the file is referenced directly from the disk and data is accessed in 64K chunks.
>
> This method is `shared</api/language/shared>`.
>
> Loading the entire *file* into memory means that the largest file that can be used is limited to the amount of free memory, up to a maximum of about 3GB or so.
>
> When the *inMemory* parameter is `False</api/language/false>`, the file is attempted to be loaded using 64K chunks. It is still possible, depending on the OS, for the entire file to be temporarily loaded into memory. This is not permanent as this memory will be released by the OS as part of its normal housecleaning.
>
> If you want to download an existing file on the server, you need to create a <span class="title-ref">WebFile</span> that points to it and save the reference. The best way to do this is to use a property of the `WebApplication</api/web/webapplication>` class. In the App.Opening event handler:
>
> ``` xojo
> Var f As FolderItem = New FolderItem("MyFile.txt", FolderItem.PathModes.Native) ' Get the file using a FolderItem
>
> If f <> Nil And f.Exists Then
>   ' Convert the FolderItem to a WebFile
>   App.MyFile = WebFile.Open(f) ' MyFile is a property on the App object
>   App.MyFile.ForceDownload = True
> End If
> ```
>
> Now in a `WebButton</api/user_interface/web/webbutton>` on a page, you can download this file like this:
>
> ``` xojo
> If App.MyFile <> Nil Then
>   Self.GoToURL(App.MyFile.URL) ' Download the file
> End If
> ```

## Event descriptions

<div id="webfile.downloaded">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebFile.Downloaded

**Downloaded**

> Fires when the current browser requests the file.
>
> Use this event to display a message telling the user that the download is underway or perhaps to navigate them to a new page.
>
> After the download is underway, send the user back to the main web page:
>
> ``` xojo
> MainPage.Show
> ```

## Notes

To get a reference to an existing `FolderItem</api/files/folderitem>`, use the `Open<webfile.open>` shared method.

If you find that FireFox users can't download files, try using `WebControl.GoToURL<webcontrol.gotourl>` and pass `True</api/language/true>` for the InNewWindow parameter as well as having the user disable popup blockers.

## Sample code

This example creates a text file to make available for download. You can put it on the Pressed event handler for a `WebButton</api/user_interface/web/webbutton>` so that the file is downloaded when the user clicks the button:

``` xojo
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!"

TextFile.Download ' This causes the file to be downloaded
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you instead want to download an existing file on the server, you need to create a <span class="title-ref">WebFile</span> that points to it and save the reference. The best way to do this is to use a property of the `WebApplication</api/web/webapplication>` class. In the App.Opening event handler:

``` xojo
Var f As FolderItem = New FolderItem("MyFile.txt", FolderItem.PathModes.Native)

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</api/user_interface/web/webbutton>` on a page, you can download this file like this:

``` xojo
If App.MyFile <> Nil Then
  Self.GoToURL(App.MyFile.URL) ' Download the file
End If
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | Web |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `WebHTMLViewer</api/user_interface/web/webhtmlviewer>`, `WebPicture</api/graphics/webpicture>`, `FolderItem</api/files/folderitem>`

</div>
