Skip to content

Upload Upload ​

Rules ​

Basic Example ​

js
const rule = {
    type: "upload",
    field: "pic",
    title: "Carousel Images",
    value: [
        'http://img1.touxiang.cn/uploads/20131030/30-075657_191.jpg',
        'http://img1.touxiang.cn/uploads/20131030/30-075657_191.jpg'
        ],
    props: {
        type:"select",
        uploadType:"image",
        action: "/upload.php",
        name:"pic",
        multiple: true,
        accept:"image/*",
        limit: 2,
        onSuccess:function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Props Configuration Examples ​

Single Image Upload ​

js
const rule = {
    type: "upload",
    field: "avatar",
    title: "Avatar",
    value: [],
    props: {
        type: "select",
        uploadType: "image",
        action: "/upload.php",
        name: "file",
        accept: "image/*",
        limit: 1,
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Multiple Image Upload ​

js
const rule = {
    type: "upload",
    field: "gallery",
    title: "Product Images",
    value: [],
    props: {
        type: "select",
        uploadType: "image",
        action: "/upload.php",
        name: "file",
        multiple: true,
        accept: "image/*",
        limit: 9,
        listType: "picture-card",
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

File Upload ​

js
const rule = {
    type: "upload",
    field: "document",
    title: "Document Upload",
    value: [],
    props: {
        type: "select",
        uploadType: "file",
        action: "/upload.php",
        name: "file",
        accept: ".pdf,.doc,.docx",
        limit: 5,
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Drag and Drop Upload ​

js
const rule = {
    type: "upload",
    field: "images",
    title: "Drag and Drop Upload",
    value: [],
    props: {
        type: "select",
        uploadType: "image",
        action: "/upload.php",
        name: "file",
        drag: true,
        multiple: true,
        accept: "image/*",
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Validation Before Upload ​

js
const rule = {
    type: "upload",
    field: "avatar",
    title: "Avatar",
    value: [],
    props: {
        type: "select",
        uploadType: "image",
        action: "/upload.php",
        name: "file",
        accept: "image/*",
        limit: 1,
        beforeUpload: function (file) {
            // Validate file size (2MB)
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isLt2M) {
                alert('Image size cannot exceed 2MB!');
                return false;
            }
            return true;
        },
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Custom Upload Request ​

js
const rule = {
    type: "upload",
    field: "file",
    title: "File Upload",
    value: [],
    props: {
        type: "select",
        uploadType: "file",
        action: "#",
        name: "file",
        httpRequest: function (options) {
            // Custom upload logic
            const formData = new FormData();
            formData.append('file', options.file);


// Upload using fetch or other methods
            fetch('/custom-upload', {
                method: 'POST',
                body: formData
            }).then(res => res.json())
            .then(data => {
                options.onSuccess(data);
            })
            .catch(err => {
                options.onError(err);
            });
        },
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        }
    },
}

Events Examples ​

Listen to Upload Events ​

js
const rule = {
    type: "upload",
    field: "pic",
    title: "Image Upload",
    value: [],
    props: {
        type: "select",
        uploadType: "image",
        action: "/upload.php",
        name: "file",
        accept: "image/*",
        onSuccess: function (res, file) {
            file.url = res.data.filePath;
        },
        onError: function (err, file) {
            console.log('Upload failed:', err);
        },
        onProgress: function (event, file) {
            console.log('Upload progress:', event.percent + '%');
        },
        onChange: function (file, fileList) {
            console.log('File state changed:', file, fileList);
        },
        onExceed: function (files, fileList) {
            console.log('Exceeded limit:', files, fileList);
            alert('File upload limit exceeded!');
        },
        onRemove: function (file, fileList) {
            console.log('File removed:', file, fileList);
        },
    },
}

Full configuration: Element_Upload

value :Array | String

Note

After file upload is successful, you need to assign the url from the interface response to file.url through the onSuccess callback. Otherwise, the form will not be able to obtain the component's data.

Props ​

NameDescriptionTypeDefault Value
action requiredRequest URLstring#
uploadTypeUpload file typeStringimage (image upload), file (file upload)
headersSet upload request headersobject—
methodSet upload request methodstringpost
multipleWhether to support multiple file selectionbooleanfalse
dataAdditional parameters attached during upload. From v2.3.13, supports Awaitable data and Functionobject / Function{}
nameField name of uploaded filestringfile
withCredentialsSupport sending cookie credential informationbooleanfalse
showFileListWhether to display uploaded file listbooleantrue
dragWhether to enable drag and drop uploadbooleanfalse
acceptAccepted upload file types (this parameter is invalid in thumbnail-mode)string''
crossoriginNative attribute crossoriginenum—
onPreviewHook when clicking uploaded file in file listFunction—
onRemoveHook when removing file from file listFunction—
onSuccessHook when file upload is successful function (res, file) {file.url = res.data.filePath;} Echo by assigning value to file.url in the callbackfunction(response, file, fileList)—
onErrorHook when file upload failsFunction—
onProgressHook during file uploadFunction—
onChangeHook when file state changes. Called when adding file, upload success and upload failureFunction—
onExceedHook function executed when limit is exceededFunction—
beforeUploadHook before uploading file. Parameter is the file to be uploaded. If it returns false or returns Promise and is rejected, upload stops.Function—
beforeRemoveHook before deleting file. Parameters are the uploaded file and file list. If it returns false or returns Promise and is rejected, deletion stops.Function—
listTypeType of file listenumtext
autoUploadAuto-upload filesbooleantrue
httpRequestOverride default Xhr behavior, allow custom implementation of file upload requestFunctionPlease refer to ajaxUpload
disabledWhether to disable uploadbooleanfalse
limitMaximum number of files allowed to uploadnumber—
modalTitleTitle text for image preview popupStringPreview
helperEnable helper methods in frame pageBooleanfalse

Slots ​

NameDescriptionType
defaultCustom default content-
triggerContent that triggers file selection box-
tipHint description text-
fileContent of thumbnail templateobject

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.