XHR
The @uppy/xhr-upload
plugin is for regular uploads to a HTTP server.
When should I use it?
Not sure which uploader is best for you? Read “Choosing the uploader you need”.
When you have an existing HTTP server and you don’t need Transloadit services or want to run a tus server. Note that it’s still possible to use tus without running an extra server by integrating tus into your existing one. For instance, if you have a Node.js server (or server-side framework like Next.js) you could integrate tus-node-server.
Install
- NPM
- Yarn
- CDN
npm install @uppy/xhr-upload
yarn add @uppy/xhr-upload
The bundle consists of most Uppy plugins, so this method is not recommended for production, as your users will have to download all plugins when you are likely using only a few.
It can be useful to speed up your development environment, so don't hesitate to use it to get you started.
<!-- 1. Add CSS to `<head>` -->
<link href="https://releases.transloadit.com/uppy/v4.13.0/uppy.min.css" rel="stylesheet">
<!-- 2. Initialize -->
<div id="uppy"></div>
<script type="module">
import { Uppy, XHRUpload } from "https://releases.transloadit.com/uppy/v4.13.0/uppy.min.mjs"
new Uppy().use(XHRUpload, { endpoint: 'https://tusd.tusdemo.net/files' })
</script>
Use
A quick overview of the complete API.
import Uppy from '@uppy/core';
import Dashboard from '@uppy/dashboard';
import XHR from '@uppy/xhr-upload';
import '@uppy/core/dist/style.min.css';
import '@uppy/dashboard/dist/style.min.css';
new Uppy()
.use(Dashboard, { inline: true, target: 'body' })
.use(XHR, { endpoint: 'https://your-domain.com/upload' });
API
Options
id
A unique identifier for this plugin (string
, default: 'XHRUpload'
).
endpoint
URL of the HTTP server (string
, default: null
).
method
Configures which HTTP method to use for the upload (string
, default:
'POST'
).
formData
Configures whether to use a multipart form upload, using FormData
(boolean
, default: true
).
This works similarly to using a <form>
element with an <input type="file">
for uploads. When set to true
, file metadata is also sent to the endpoint as
separate form fields. When set to false
, only the file contents are sent.
fieldName
When formData
is set to true, this is used as the form field
name for the file to be uploaded.
It defaults to 'files[]'
if bundle
option is set to true
, otherwise it
defaults to 'file'
.
allowedMetaFields
Pass an array of field names to limit the metadata fields that will be added to upload.
- Set it to
false
to not send any fields (or an empty array). - Set it to
['name']
to only send thename
field. - Set it to
true
(the default) to send all metadata fields.
If the formData
option is set to false, metaFields
is
ignored.
headers
An object containing HTTP headers to use for the upload request. Keys are header names, values are header values.
const headers = {
authorization: `Bearer ${window.getCurrentUserToken()}`,
};
Header values can also be derived from file data by providing a function. The function receives an Uppy file and must return an object where the keys are header names, and values are header values.
const headers = (file) => {
return {
authorization: `Bearer ${window.getCurrentUserToken()}`,
expires: file.meta.expires,
};
};
The function syntax is not available when bundle
is set to true
.
Failed requests are retried with the same headers. If you want to change the
headers on retry,
such as refreshing an auth token,
you can use onBeforeRequest
.