# journey.files

{% hint style="info" %}
**Version compatibility**

`journey.files` was introduced with version **4.80.0** of the JourneyApps Runtime.
{% endhint %}

Ability to interact with files in JourneyApps.

<table><thead><tr><th width="250">Property</th><th width="395.59370596524184">Description</th><th>Example</th></tr></thead><tbody><tr><td><code>saveFile</code></td><td><p>Saves a file to the device.</p><p><strong>Note:</strong> Due to limitations on mobile file systems, this method is only supported on Desktop and Web.</p></td><td><a href="#journey.files.savefile">See below</a></td></tr><tr><td><code>viewFile</code></td><td><p>Opens a file viewer in-app to display an attachment.</p><p><strong>Note:</strong> Supported file types: <code>.jpg/.jpeg</code>, <code>.png</code>, and <code>pdf</code>.</p></td><td><a href="#journey.files.viewfile">See below</a></td></tr><tr><td><code>openFileExternally</code></td><td><p>Opens the default file viewer (based on media type) of the device to display an attachment. Supported file types for RealWear: <code>.jpg/.jpeg</code>, <code>.png</code>, <code>.bmp</code>, <code>.webp</code>, <code>.gif</code>, <code>.pdf</code>.</p><p><strong>Note:</strong></p><ul><li>Supported in JourneyApps Container version <strong>22.2.1</strong> and Runtime version <strong>4.84.0</strong> and later.</li><li>This method is currently not supported on Web.</li></ul></td><td><a href="#journey.files.openfileexternally">See below</a></td></tr></tbody></table>

### Examples

#### `journey.files.saveFile`

`journey.files.viewFile(data, filename)` where `data` is the `string` or `ArrayBuffer` to save to the file, and `filename` is a `string` containing the name of the file created.

Saving a `string`:

```javascript
function saveText() {
    var text = "Text to save!";
    journey.files.saveFile(text, 'my-file.txt');
}
```

Saving a `photo`:

```xml
<var name="site_entrance" type="photo" />

<capture-photo label="Site entrance" bind="site_entrance" source="camera" resolution="small" downloadable="true" />
<button show-if="site_entrance" label="Save photo" on-press="$:savePhoto()" validate="false" />
```

```javascript
function savePhoto() {
    var buffer = view.site_entrance.toArrayBuffer();
    journey.files.saveFile(buffer, "site_entrance.jpg");
}
```

#### `journey.files.viewFile`

```xml
<var name="supporting_document" type="attachment" media="application/pdf" />

<capture-file label="Upload supporting document (PDF)" bind="supporting_document" downloadable="true" />
<button show-if="supporting_document" label="View File" on-press="$:viewFile()" validate="false" />
```

```javascript
function viewFile() {
    journey.files.viewFile(view.supporting_document);
}
```

#### `journey.files.openFileExternally`

```xml
<var name="supporting_document" type="attachment" media="application/pdf" />

<capture-file label="Upload supporting document (PDF)" bind="supporting_document" downloadable="true" />
<button show-if="supporting_document" label="Open File Externally" on-press="$:openExternally()" validate="false" />
```

```javascript
function openExternally() {
    try { 
        journey.files.openFileExternally(view.supporting_document); 
    } catch(e) { 
        // Fallback to viewFile 
        // Example scenario: This could occur if the app does not have file system permissions
        // Note that the container will throw an error if a user tries to call this method on an unsupported container version. We recommend performing a container version check as well.
        journey.files.viewFile(view.supporting_document)
    }
}
```

{% hint style="info" %}
**TypeScript apps**

* `journey.files.openFileExternally` requires version **2.0.2** or greater of the [`@journeyapps/runtime-build` package](https://docs.journeyapps.com/reference/build/syntax/typescript-apps/runtime-build-package).
  {% endhint %}

{% hint style="info" %}
**Synced state**

Ensure that the `attachment` is synced, by calling `journey.synchronize()` before either `journey.files.viewFile` or `journey.files.openFileExternally`.
{% endhint %}
