# `@filerobot/xhr-upload`

[![Plugins][plugins-image]](#plugins)
[![Website][filerobot-image]][sfx-url]
[![Version][filerobot-version]][version-url]
[![Scaleflex team][made-by-image]][sfx-url]
[![License][license-image]][license-url]
[![CodeSandbox][codeSandbox-image]][codeSandbox-url]

<div align='center'>
<img title="Filerobot Widget logo" alt="Filerobot Widget logo" src="https://cdn.scaleflex.com/plugins/filerobot-widget/assets/filerobot_widget_logo_with_fire.png?vh=b2ff09" width="140"/>
</div>

The XHR upload plugin for [Filerobot Media Asset Widget](https://www.npmjs.com/package/@filerobot/readme) handles uploading files in Multipart form uploading way and HTTP(s) protocol.

## Usage

### NPM

```bash
npm install --save @filerobot/xhr-upload
```

### YARN

```bash
yarn add @filerobot/xhr-upload
```

then

```js
import XHRUpload from '@filerobot/xhr-upload'
...
...
...
filerobot.use(XHRUpload, propertiesObject)
```

### CDN

The plugin from CDN is found inside `Filerobot` global object `Filerobot.XHRUpload`

```js
const XHRUpload = window.Filerobot.XHRUpload
...
...
...
filerobot.use(XHRUpload, propertiesObject)
```

### Example

You can use upload in two ways:

- By using [Progress Panel](../@filerobot/progress-panel#Example) plugin, please check the example there.
- By using widget events, please check how to use events [here](..@filerobot/core/#upload) and see the example below:

```js
const Filerobot = window.Filerobot;
const demoContainer = "scaleflex-tests-v5a";
const demoSecurityTemplateId = "......";

const filerobot = Filerobot.Core({
  securityTemplateId: demoSecurityTemplateId,
  container: demoContainer,
  dev: false, // optional, default: false
});

const Explorer = Filerobot.Explorer;
const XHRUpload = Filerobot.XHRUpload;

filerobot
  .use(Explorer, { target: "#filerobot-widget", inline: true })
  .use(XHRUpload);

// events
filerobot.on("upload", () => {
  // trigger when upload is clicked inside explorer
});
filerobot.on("upload-started", () => {
  // trigger when upload starts
});
filerobot.on("upload-progress", () => {
  // trigger when upload is progressing
});
filerobot.on("upload-success", () => {
  // trigger when upload is completed
});
filerobot.on("upload-error", () => {
  // trigger when upload has an error
});
```

## Properties

### `uploadToFolderPath`

<u>Type:</u> `string`.

<u>Default:</u> `null`

The path of the folder that will be used in uploading to, if this property is provided it will override the current opened folder path and files would be uploaded to this path.

> Note: you shouldn't provide `folder=...` in the `uploadQueryParams` if you want to use this property or `uploadQueryParams` will override this property's value.

### `uploadQueryParams: string`

<u>Type:</u> `string`.

<u>Default:</u> `''`

The query to be appended to the upload url, ex. `../upload?`**`extra=hello&foo=bar`**.

### `limit`

<u>Type:</u> `number`.

<u>Default:</u> `0`

Limit the number of uploads' requests handled at the same if left `0` then there is no limit.

### `timeout`

<u>Type:</u> `number`.

<u>Default:</u> `60 * 1000`

Defines the timeout for not receiving upload events or having no uploading's progress after that amount of milliseconds it abort the upload assuming there is a problem with the connection, if set to `0` this feature will be disabled.

### `headers: `

<u>Type:</u> `object`.

<u>Default:</u> `{X-Filerobot-Key: '...'}`

If you want to customize the headers of the upload requests are being sent to the backend.

### `info`

<u>Type:</u> `object`.

<u>Default:</u> `undefined`

If you want to pass some info to the file pass an object with the needed info to this property and it would be considered while uploading the file, example: `{ recipe_id: 10 }`.

> Note: Applied only for local uploads <!-- maybe, because the backend doesn't support all info data fields? -->

### `addInfoCallback`

<u>Type:</u> `function`.

<u>Default:</u> `undefined`

If you want to pass some info to the file after doing some operation/functionality, pass a function that has `file` object as a parameter that contains the file's details and returns an object of the info data to be passed and saved to the file while uploading otherwise the function won't be considered.

Example, `(file) => (file.name === 'first' ? { file_order: 'first' } : false)`

> Note: Applied only for local uploads <!-- maybe, because the backend doesn't support all info data fields? -->

### `meta`

<u>Type:</u> `object`.

_default_: `undefined`

Add some meta in the file object by providing the meta as object to this property , example: `{ recipe_category: 'Vegetables' }`.

> Note: Applied only for local uploads

### `locale`

<u>Type:</u> `object`.

<u>Default:</u>

```js
{
  strings: {
    timedOut: "Upload stalled for %{seconds} seconds, aborting.";
  }
}
```

Customizing some of the translations or the language's strings and replace the default locale.

<!-- Variables -->

[npm-url]: https://www.npmjs.com/package/@filerobot/xhr-upload
[license-url]: https://opensource.org/licenses/MIT
[sfx-url]: https://www.scaleflex.com/
[version-url]: https://www.npmjs.com/package/@filerobot/xhr-upload
[codeSandbox-url]: https://codesandbox.io/s/filerobot-widget-v3-c5l9th
[npm-image]: https://img.shields.io/npm/v/@telus/remark-config.svg?style=for-the-badge&logo=npm
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge
[made-by-image]: https://img.shields.io/badge/%3C%2F%3E%20with%20%E2%99%A5%20by-the%20Scaleflex%20team-6986fa.svg?style=for-the-badge
[plugins-image]: https://img.shields.io/static/v1?label=Filerobot&message=Plugins&color=yellow&style=for-the-badge
[filerobot-image]: https://img.shields.io/static/v1?label=Filerobot&message=website&color=orange&style=for-the-badge
[filerobot-version]: https://img.shields.io/npm/v/@filerobot/xhr-upload?label=Version&style=for-the-badge&logo=npm
[codeSandbox-image]: https://img.shields.io/badge/CodeSandbox-black?style=for-the-badge&logo=CodeSandbox
