Javascript options
JavaScript options¶
Signature of initUploadFields is:
initUploadFields(formDomElement, options);
formDomElement(required); for exampledocument.getElementById("example-form")- options (optional)
callbacks: callbacks for things like upload progress and errors.chunkSize: the maximum size of an upload. Default is 2.5 MB.- Note that Django limits the size of a request. See https://docs.djangoproject.com/en/4.1/ref/settings/#data-upload-max-memory-size.
prefix: set this if the Django form has a prefix; default is emptyretryDelays: set retry delays- The wait times in milliseconds before retrying a failed request; there is one retry for each value.
- Applies to tus uploads and to the parts of an S3 upload.
- Also see
https://github.com/tus/tus-js-client#tusdefaultoptions - Default is
[0, 1000, 3000, 5000]
skipRequired: don't set therequiredfield of the file input; default isfalsesupportDropArea: add a drop area; default istrue
The callbacks are:
-
onClick -
Called when the filename of an uploaded file is clicked
-
Signature of callback is
function({ fileName, fieldName, id, type })fileName: the filenamefieldName: the name of the form field- id: the id of the upload;
undefinedfor a placeholder or an existing file - For a tus upload, this is the id of the temporary uploaded file.
- For an S3 upload, this is the id of the S3 multipart upload.
- type:
tusoruploadedTus: a tus uploads3oruploadedS3: an S3 uploadplaceholder: a placeholderexisting: an existing file- The filename is rendered with the
dff-filenameclass. You can use this class to change the styling. - When this callback is set, the filename of an uploaded file is rendered as a
<button>, so that it can also be clicked using the keyboard. While a file is uploading, the filename is plain text.
-
onDelete - Called when file is deleted
- Signature of callback is
function(upload) onError:- Called when an upload error occurs; for tus and S3 uploads
- Signature of callback is
function(error, upload)error: a JavaScriptError
onProgress:- Called each time when progress information is available; for tus and S3 uploads
- Signature of callback is
function(bytesUploaded, bytesTotal, upload) onSuccess:- Called when file upload is done; for tus and S3 uploads
- Signature of callback is
function(upload)
The upload parameter of the callbacks is an upload object of django-file-form. It has these properties and methods:
name: the file nametype: the kind of uploadtus: a tus uploads3: an S3 uploaduploadedTusoruploadedS3: a file that was uploaded before the form was rendered, for example when the form had validation errorsplaceholder: a placeholderexisting: an existing filestatus:uploading,doneorerroruploadIndex: a number that identifies the upload within its form fieldgetId(): the id of the upload on the server;undefinedwhen the upload isn't created yetgetSize(): the size of the file in bytes, if it's known
onDelete can receive any kind of upload; onError, onProgress and onSuccess receive a tus or s3 upload.
Examples:
initUploadFields(document.getElementById("example-form"));
initUploadFields(document.getElementById("example-form"), {
prefix: "example",
skipRequired: true,
supportDropArea: true,
});