# File Uploader

See [VGSFileProvider](https://verygoodsecurity.github.io/vgs-collect-android/vgscollect/com.verygoodsecurity.vgscollect.core.storage.content.file/-v-g-s-file-provider/index.html) , [FileState](https://verygoodsecurity.github.io/vgs-collect-android/vgscollect/com.verygoodsecurity.vgscollect.core.model.state/-file-state/index.html) in API references.

`VGSFileProvider` is a part of VGS Collect Android SDK. The instance provides means for file management inside the SDK. It allows managing files inside the SDK by `attach`, `detach` and `get` file general info methods.

To manage the files in your application, you need to use `VGSFileProvider`. To get it, call getFileProvider() from your `VGSCollect` object.

There are a few actions that may be of interest for managing files:

* **Save** file with `attachFile()` for sending in the future to the Proxy Server.
* **Remove** one by one or all together using `detachFile()` or `detachAll()`.
* **Overview** of all already attached files with `getAttachedFiles`.

Also, don't forget to implement `onActivityResult` callback.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
private val vgsForm = VGSCollect(this, "<VAULT_ID>", Environment.<SANDBOX> )\n
fun getFileProvider() {
    val fileProvider: VGSFileProvider = vgsForm.getFileProvider()
}\n
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    vgsForm.onActivityResult(requestCode, resultCode, data)
}
```

{% endtab %}

{% tab title="Java" %}

```java
private VGSCollect vgsForm = new VGSCollect(this, "<VAULT_ID>", Environment.<SANDBOX> );\n
fun getFileProvider() {
    VGSFileProvider fileProvider = vgsForm.getFileProvider();
}\n
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
    super.onActivityResult(requestCode, resultCode, data);
    vgsForm.onActivityResult(requestCode, resultCode, data);
}
```

{% endtab %}
{% endtabs %}

## Limitations

Please note, for now, there are some limitations on the data you can upload:

* **Maximum upload file size**: files should not exceed **20MB**.
* **Maximum file count**: only one file at a time can be selected and submitted.

## Attach file

Register a file to be uploaded when your request is finished and sent to the server. Before attaching a file using the SDK, you should define `fieldName`. Usually, it is similar to a field name in the JSON path in your inbound route filters.

After `attachFile` is called, Android will open your device's content provider to choose a file.

> Don't forget to detach the file after submitting it to the VGS Proxy. After the file was attached, it stays in local storage and is send in every request until you perform final cleanup by calling `onDestroy()`.

{% tabs %}
{% tab title="Kotlin" %}

<pre class="language-kotlin"><code class="lang-kotlin"><strong>private val vgsForm = VGSCollect( this, "&#x3C;VAULT_ID>", Environment.&#x3C;SANDBOX> )\n
</strong>private fun attachFile() {
    val fileProvider: VGSFileProvider = vgsForm.getFileProvider()
    fileProvider.attachFile(activity, "&#x3C;field_name>")
}
</code></pre>

{% endtab %}

{% tab title="Java" %}

```java
private VGSCollect vgsForm = new VGSCollect( this, "<VAULT_ID>", Environment.<SANDBOX> );\n
private void attachFile() {
    VGSFileProvider fileProvider = vgsForm.getFileProvider();
    fileProvider.attachFile(activity, "<field_name>");
}
```

{% endtab %}
{% endtabs %}

## Get attached files

This method is used to get attached files to review them before sending. It returns all states of the files that are attached to the `VGSCollect` instance.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
private val vgsForm = VGSCollect( this, "<VAULT_ID>", Environment.<SANDBOX> )\n
private fun getAttachedFiles() {
    val fileProvider: VGSFileProvider = vgsForm.getFileProvider()
    val states:List<FileState> = fileProvider.getAttachedFiles()
}
```

{% endtab %}

{% tab title="Java" %}

```java
private VGSCollect vgsForm = new VGSCollect( this, "<VAULT_ID>", Environment.<SANDBOX> );\n
private void getAttachedFiles() {
    VGSFileProvider fileProvider = vgsForm.getFileProvider();
    List<FileState> states = fileProvider.getAttachedFiles();
}
```

{% endtab %}
{% endtabs %}

## Detach file

You can detach one or all files attached to the SDK earlier.

To detach one separate file call the `detachFile` method with the `FileState` object as a parameter. Also, you can detach all files at once by `detachAll`.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
private val vgsForm = VGSCollect( this, "<VAULT_ID>", Environment.<SANDBOX> )\n
private fun detachFile(fileInfo:FileState) {
    val fileProvider: VGSFileProvider = vgsForm.getFileProvider()
    fileProvider.detachFile(fileInfo)
}\n
private fun detachAll() {
    val fileProvider: VGSFileProvider = vgsForm.getFileProvider()
    fileProvider.detachAll()
}
```

{% endtab %}

{% tab title="Java" %}

```java
private VGSCollect vgsForm = new VGSCollect( this, "<VAULT_ID>", Environment.<SANDBOX> );\n
private void detachFile(FileState fileInfo) {
    VGSFileProvider fileProvider = vgsForm.getFileProvider();
    fileProvider.detachFile(fileInfo);
}\n
private fun detachAll() {
    VGSFileProvider fileProvider = vgsForm.getFileProvider();
    fileProvider.detachAll();
}
```

{% endtab %}
{% endtabs %}
