# JSUpload

## Overview

The `JSUpload` class provides robust tools for managing uploaded files in Servoy applications. It supports accessing file contents as bytes, strings, or input streams and provides metadata retrieval for handling uploads. Developers can determine if files are stored in memory or on disk and write them to specified locations using the `write` method, which manages temporary files. Metadata associated with uploads, such as form field names and their values, can be accessed using the `getFields` and `getFieldValue` methods. The class also allows retrieval of file-specific details, including size, name, and content type, while ensuring compatibility with browsers that may include full file paths. The `deleteFile` method explicitly removes temporary files to free resources, complementing automatic cleanup processes. By combining file content management with metadata handling, `JSUpload` offers a streamlined solution for file upload operations in Servoy applications.

## Methods Summarized

| Type                                                       | Name                                       | Summary                                                                                                                                              |
| ---------------------------------------------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| void                                                       | [deleteFile()](#deletefile)                | Delets this uploaded file so it will be cleaned up if it was streamed in a temp file.                                                                |
| [Array](/reference/servoycore/dev-api/js-lib/array.md)     | [getBytes()](#getbytes)                    | This returns the bytes of the uploaded file, try to using streaming or file operation on it (so the bytes don't have to be full loaded in to memory) |
| [String](/reference/servoycore/dev-api/js-lib/string.md)   | [getContentType()](#getcontenttype)        |                                                                                                                                                      |
| [String](/reference/servoycore/dev-api/js-lib/string.md)   | [getFieldValue(name)](#getfieldvalue-name) | Returns the value for a give form field that was give as metadata to this uploaded file                                                              |
| [Array](/reference/servoycore/dev-api/js-lib/array.md)     | [getFields()](#getfields)                  | This returns the field names of the form fields that where give as metadata to this upload file.                                                     |
| [String](/reference/servoycore/dev-api/js-lib/string.md)   | [getName()](#getname)                      |                                                                                                                                                      |
| [Number](/reference/servoycore/dev-api/js-lib/number.md)   | [getSize()](#getsize)                      |                                                                                                                                                      |
| [String](/reference/servoycore/dev-api/js-lib/string.md)   | [getString()](#getstring)                  | Returns the contents of the file as as string in UTF-8 encoding.                                                                                     |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) | [isInMemory()](#isinmemory)                | If this returns false, then a tmp file is created for it.                                                                                            |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) | [write(file)](#write-file)                 | Writes the contents of this upload right to a file.                                                                                                  |

## Methods Detailed

### deleteFile()

Delets this uploaded file so it will be cleaned up if it was streamed in a temp file.\
The system tries to clean this up for you, but that can take a while and depends on Garbage Collection.\
So it is better to be explicit and delete this file.\
if you use JSUpload.write(file) then the file is very likely moved instead of copied so the temp file is also removed.

**Returns:** void

### getBytes()

This returns the bytes of the uploaded file, try to using streaming or file operation on it (so the bytes don't have to be full loaded in to memory)

**Returns:** [Array](/reference/servoycore/dev-api/js-lib/array.md) the bytes of the upload file,

### getContentType()

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) the content type of this upload

### getFieldValue(name)

Returns the value for a give form field that was give as metadata to this uploaded file

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **name** The form fields name

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) the value that was given or null

### getFields()

This returns the field names of the form fields that where give as metadata to this upload file.

**Returns:** [Array](/reference/servoycore/dev-api/js-lib/array.md) String\[] Array of names of the field names

### getName()

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) the name of the upload file.

### getSize()

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) the size of the upload

### getString()

Returns the contents of the file as as string in UTF-8 encoding.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) the String contents

### isInMemory()

If this returns false, then a tmp file is created for it. This means that you can also convert this to a JSFile and call rename() on it.\
But the method write(file) will always work by writing the contents of this upload file to a different file.

**Returns:** [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) true if this upload is fully in memory (not saved to a temp file)

### write(file)

Writes the contents of this upload right to a file. Use the file plugin to create a JSFile object that can be given to this function.\
If this file was not fully in memory (isInMemory == false) then this will just stream the tmp file to the give file.\
If it was a temp file then it will try to move the file to the given location (so temp file is moved and because of that already deleted/cleaned up).

**Parameters**

* [Object](/reference/servoycore/dev-api/js-lib/object.md) **file** the file object where to write to can be a JSFile or path string

**Returns:** [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) if write could be done

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/dev-api/application/jsupload.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
