# Create file object

The Create file object action node adds files into a [data source](https://docs.appfarm.io/reference/apps/data/data-sources) based on a [file object class](https://docs.appfarm.io/reference/data-model/object-classes#file-object-class). For static files used in your apps, see [Files](https://docs.appfarm.io/reference/resources/files).

## Usage

This action node is used to create a new file [object](https://docs.appfarm.io/reference/platform-concepts/objects) containing a file and associated metadata. To understand the differences in behavior when creating an object in a database-connected data source compared to a runtime-only data source, see [Database-connected and runtime-only data sources](https://docs.appfarm.io/reference/apps/data/data-sources#database-connected-and-runtime-only-data-sources) under [Data sources](https://docs.appfarm.io/reference/apps/data/data-sources).

A file can be added from several sources:

* The file system of a client user's computer or mobile device.
* The camera on a client user's mobile device (image or video).
* A URL.
* A string or blob.

When uploading files from the file system, the action node supports multiple simultaneous uploads using the **Max Selectable Files** property. This allows a client user to select multiple files in the file browser, or to drag and drop multiple files on a [container](https://docs.appfarm.io/library/ui-components/container) configured for file drop.

Images can be resized (with **Max Image Size**) and compressed (with **Compress Image**) on upload in order to control file sizes. When these features are enabled, images will be converted to JPEG format. Transparency is not supported and any transparent parts of an image will become black.

By default, files in a file object require authentication to access. If required, a file can be made available for public distribution to unauthenticated users by selecting the **Public** property.

When adding a file from a URL, both public and authenticated URLs are supported. Secrets can be used in either the URL or a request header by selecting the **Run on Server** property.

{% hint style="info" %}
**Example**

An example of Create file object is available in our [Showroom](https://showroom.appfarm.app/action-nodes/create-file-object). You can view the demo, as well as access the setup in Appfarm Create. Don't have access? [Register](https://showroom.appfarm.app/sign-up).
{% endhint %}

{% hint style="warning" %}
**Is the file browser or camera not opening?**

The Create File action node, with Source Type Default (opening the file browser or device camera on your phone), is in some cases required to be the *first action node* of the action in order for the browser to interpret the operation as "user-initiated". This has been seen in Safari on iPhone. The solution is to move the Create File Object as the first action node, and then do additional logic.
{% endhint %}

### Create file from code

Create File Object can be used to generate files programmatically, when **Source Type** `Custom` is used. In this case, you must define the **Mime Type** as well (see [Properties](#properties) below).

Examples:

* Generate a complex CSV file using the function editor, creating the CSV content as a string. See CSV file example code [here](https://docs.appfarm.io/how-to/integrations/integrate-with-openai#analyze-business-data-or-files-on-demand). Mime Type: `text/csv`.
* Generate a PDF from a base64 string: Simply use the function `return a2b(<base64string here>)` and Mime Type `application/pdf`. This works in both Apps and Services.
* Generate any other file type you may want to, using a function to return the code. You may also refer external javascript libraries when Create file objects is used in Apps (not Services), by adding [Custom Header Tags.](https://docs.appfarm.io/reference/configuration/environments#custom-header-tags)

## Properties

<table><thead><tr><th width="236">Property</th><th>Description</th></tr></thead><tbody><tr><td><strong>Data Source</strong></td><td>Select a data source based on a <a href="../../../reference/data-model/object-classes#file-object-class">file object class</a> to create the object in.</td></tr><tr><td><strong>Source Type</strong></td><td><p>Select the source of the file.</p><ul><li><code>Default</code>: Open the file browser on the user's device, or launch the camera if <strong>Use Device Camera</strong> is selected.</li><li><code>URL</code>: Fetch a file from a URL.</li><li><code>Custom</code>: Set the file contents as a string or blob with a custom MIME type. </li></ul></td></tr><tr><td><strong>Max Selectable Files</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Default.</em><br><br>Set a maximum number of files that can be uploaded in one operation. If a user attempts to upload more than this number, an error will be thrown. Default is 1.</td></tr><tr><td><strong>Accepted File Types</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Default.</em><br><br>Select the file types that are supported in the app. This will be used in the file browser to disable unaccepted file types. If a user attempts to paste or drop a file, unaccepted types will be filtered out. By default, all file types are accepted.</td></tr><tr><td><strong>Max Image Size</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Default and</em> <strong>Accepted File Types</strong> <em>includes Image.</em><br><br>Set a maximum size in pixels that will be applied to an image when it is uploaded. If an image exceeds this size, it will be resized to the maximum.<br><br>When a value is set for this property, all images will be converted to JPEG format. Transparency is not supported and any transparent parts of the image will become black.</td></tr><tr><td><strong>Compress Image</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Default and</em> <strong>Accepted File Types</strong> <em>includes Image.</em><br><br>Apply compression to the image when it is uploaded. This process converts images to JPEG format. Transparency is not supported and any transparent parts of the image will become black.</td></tr><tr><td><strong>Use Device Camera</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Default and</em> <strong>Accepted File Types</strong> <em>includes Image or Video.</em><br><br>Launch the camera on a mobile device instead of the file browser. On computers, the file browser will be opened.</td></tr><tr><td><strong>URL</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is URL.</em><br><br>Set the URL where the file is located.</td></tr><tr><td><strong>Request Headers</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is URL.</em><br><br>Add request headers if any are required to fetch the file from the URL.</td></tr><tr><td><strong>Run On Server</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is URL.</em><br><br>Run the request to fetch the file from the URL on the server instead of the client. This can be useful to avoid <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">CORS</a> problems or when you need to use a <a href="../../reference/security/secrets">secret</a> in the request.</td></tr><tr><td><strong>Replace Existing Object</strong></td><td><em>Available if the selected</em> <strong>Data Source</strong> <em>is a single-cardinality data source.</em> <br><br>Remove any existing object in the selected <strong>Data Source</strong> before the new object is created.</td></tr><tr><td><strong>Set Selected</strong></td><td>Set the newly created objects as <a href="../../../reference/platform-concepts/objects#object-selection">selected</a>.</td></tr><tr><td><strong>Public</strong></td><td>Make the file available for public distribution to unauthenticated users. Additionally, this option can be useful when you don't want the <a href="../../../reference/data-model/object-classes#built-in-properties-for-file-object-classes">File Content URL</a> of a persisted file object to expire. Read more about URL lifetime <a href="../../../reference/data-model/object-classes#url-lifetime">here</a>.</td></tr><tr><td><strong>Custom File Content</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Custom.</em><br><br>Set a string or blob to use as the contents of the file.</td></tr><tr><td><strong>File Name</strong></td><td><p><em>Available unless</em> <strong>Max Selectable Files</strong> <em>is greater than 1.</em><br></p><p>Set a file name that will be applied if the file is downloaded by a user. This value is stored in the <a href="../../../reference/data-model/object-classes#built-in-properties-for-file-object-classes">object class property</a> <strong>Original File Name.</strong></p></td></tr><tr><td><strong>Mime Type</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Custom.</em><br><br>Set a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types">MIME type (media type)</a> for a custom file.</td></tr><tr><td><strong>Values on create</strong></td><td>Set values for any object class properties in the new object.</td></tr></tbody></table>
