npm package: @scaleflex/widget-xhr-upload
Description: Plain and simple classic HTML multipart form uploads with Scaleflex Media Asset Widget, as well as uploads using the HTTP PUT method.
Latest version: 4.8.13

# `@scaleflex/widget-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="Scaleflex Widget logo" alt="Scaleflex Widget logo" src="https://assets.scaleflex.com/Corporate+Branding/%5B2025%5D+ALL+LOGOS+%2B+ICONS/SCALEFLEX/VXP+logo/Horizontal+White/VXP+logo+WHITE.png?vh=663932" width="140"/>
</div>

The XHR upload plugin for [Scaleflex Media Asset Widget](https://www.npmjs.com/package/@scaleflex/widget-core) handles uploading files in Multipart form uploading way and HTTP(s) protocol.

## Usage

### NPM

```bash
npm install --save @scaleflex/widget-xhr-upload
```

### YARN

```bash
yarn add @scaleflex/widget-xhr-upload
```

then

```js
import XHRUpload from '@scaleflex/widget-xhr-upload'
...
...
...
scaleflexWidget.use(XHRUpload, propertiesObject)
```

### CDN

The plugin from CDN is found inside `Scaleflex` global object `Scaleflex.XHRUpload`

```js
const XHRUpload = window.ScaleflexWidget.XHRUpload
...
...
...
scaleflexWidget.use(XHRUpload, propertiesObject)
```

### Example

You can use upload in two ways:

- By using [Progress Panel](../@scaleflex/widget-progress-panel#Example) plugin, please check the example there.
- By using widget events, please check how to use events [here](..@scaleflex/widget-core/#upload) and see the example below:

```js
const ScaleflexWidget = window.ScaleflexWidget;
const demoContainer = "scaleflex-tests-v5a";
const demoSecurityTemplateId = "......";

const scaleflexWidget = ScaleflexWidget.Core({
  securityTemplateId: demoSecurityTemplateId,
  container: demoContainer,
  dev: false, // optional, default: false
});

const Explorer = ScaleflexWidget.Explorer;
const XHRUpload = ScaleflexWidget.XHRUpload;

scaleflexWidget
  .use(Explorer, { target: "#scaleflex-widget", inline: true })
  .use(XHRUpload);

// events
scaleflexWidget.on("upload", () => {
  // trigger when upload is clicked inside explorer
});
scaleflexWidget.on("upload-started", () => {
  // trigger when upload starts
});
scaleflexWidget.on("upload-progress", () => {
  // trigger when upload is progressing
});
scaleflexWidget.on("upload-success", () => {
  // trigger when upload is completed
});
scaleflexWidget.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-Scaleflex-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/@scaleflex/widget-xhr-upload
[license-url]: https://opensource.org/licenses/MIT
[sfx-url]: https://www.scaleflex.com/
[version-url]: https://www.npmjs.com/package/@scaleflex/widget-xhr-upload
[codeSandbox-url]: https://codesandbox.io/p/sandbox/scalelfex-widget-v4-sandbox-dtp6l3?file=%2Fsrc%2Findex.js
[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=Scaleflex&message=Plugins&color=yellow&style=for-the-badge
[filerobot-image]: https://img.shields.io/static/v1?label=Scaleflex&message=website&color=orange&style=for-the-badge
[filerobot-version]: https://img.shields.io/npm/v/@scaleflex/widget-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