Skip to main content

File Upload

WdUploadFile

Applicable Scenarios

For uploading local file resources, such as video, audio, PDF, Excel, Word, and other file types.

Basic Capabilities

Binding "File, Array" Fields

  1. Single file upload: After the form container is bound to the data model, the "File" field in the model is automatically rendered as a file upload component, enabling support for single file upload.
  2. Multiple file upload: After the form container is bound to the data model, the "Array" field of image type in the model is automatically rendered as a file upload component, enabling support for multiple file uploads.

Extended Scenarios

Get the File ID After User Upload

After a file is uploaded via the file upload component, the "Upload Success" event is triggered. The cloudID of the uploaded file can be obtained from the event.detail.value parameter of this event. This ID can be viewed by printing logs, as shown in the following example:

  1. Create a custom method and write the log printing code console.log(event.detail.value). The specific content format is as shown in the figure below:

  2. Drag and drop the "File Upload" component into the page, and configure the "Upload Success" event to trigger this log printing method.

  3. Preview the configuration effect: you will find that after the file is uploaded successfully, the cloudID of the uploaded file will be printed in the console.

  4. In daily use, you can store this ID in a variable through the component's upload success event, or utilize it in other ways.

Other Scenario Practices

Refer to the Form Scenario Practice Guide to learn about various supported scenarios and implementation solutions for forms.

Notes

caution

After a file is uploaded, it is downloadable by the logged-in user by default. For security reasons, the generated temporary download URL has a time limit and is not recommended for storage in databases. To restrict file/image resources to be downloadable only by the uploader, adjust the bucket access permission to "Only creator and administrator have read-write access" in the Cloud Development - Cloud Storage module of the WeDa environment.

Example

Interactive Preview

Component Input State

Style API Example

#wd-page-root .wd-form-item.wd-pc-upload-file-root .wd-upload-file__label {
color: #05c3c3;
display: flex;
justify-content: center;
align-items: center;
}
#wd-page-root
.wd-form-item.wd-pc-upload-file-root
.weda-upload-file-pc__btn--weak {
background: transparent;
}

Properties

External properties received by the component

Property Name
Property Identifier
Type
Description
显示标题labelVisibleboolean

Default value: true

标题对齐labelAlignstring

表单场景下默认会跟随表单容器的标题对齐配置

标题换行labelWrapboolean

如果标题内容过长关闭时只显示一行、溢出省略;开启时换行展示。表单场景下默认会跟随表单容器的标题换行配置

标题位置layoutstring

设置标题在表单组件的展示位置,表单场景下默认会跟随表单容器的标题的位置配置

标题宽度labelWidthstring

您可以输入数值 + px、%等单位,示例:200px

表单场景下默认会跟随表单容器的标题宽度配置

标题提示labelTipsstring

PC/H5端生效

配置标题的工具提示内容

单个文件最大限制(M)maxSizenumber

Default value: 10

文件类型限制acceptTypesstring

为空表示不做限制;多个类型之间请用英文逗号分隔,如 doc,pdf,png;该属性暂不支持小程序。

支持上传的文件类型格式

文件数量上限maxUploadCountnumber

允许上传的最大数量

Default value: 9

状态statusstring

Example: "edit"

必填requiredboolean
必填标识requiredFlagboolean

启用后,组件若要求必填,则会显示必填星号标记

Default value: true

必填校验提示requiredMsgstring

Example: "该项为必填项"

上传按钮文案uploadButtonTextstring

上传按钮文案

Default value: "点击上传"

批量上传描述uploadTipTextstring

支持批量上传时的文案描述

Default value: "支持批量上传"

下方提示extrastring

配置后提示内容常显在输入框下方

上传后支持下载downloadVisibleboolean

Default value: true

上传后支持删除deleteVisibleboolean

Default value: true

移动端显示下划线borderedH5boolean

关闭后移动端不显示底部下划线

Default value: true

callbacksobject

配置相关的函数

绑定字段namestring

文件上传后,文件 https 链接默认仅可在微搭应用中访问,有效期 2 小时,可在云存储权限设置中调整访问权限。

表单字段的Key值,用于提交数据时,匹配数据模型字段标识。表单内需保证唯一。

标题内容labelstring

Example: "文件上传"

值存储方式storageTypestring

选择cloudID方式,文件将以云存储 fileID 的形式直接存储在数据模型中;选择https方式,需先开启云存储公有读写权限,文件将以https链接形式存储在数据模型中。

指定图片存储方式

Default value: "cloudID"

文件值valuearray

支持以数组格式写入文件的https地址或cloudID;上传单个时支持字符串类型,上传多个时支持数组类型

上传单个文件singleboolean

如需上传多个文件,可关闭该属性,但需绑定数组-文件字段,以保证表单的正常提交 了解详情

开启小程序端获取微信头像后,仅能上传1张图片

Pre-upload Handler Function Property

The uploaded files can be preprocessed via the pre-upload handler function.

Function Input Parameter Type:

type BeforeUploadParams0 = {
// base64 uri
base64Uri: string[];
// Exclusive to the Mini Program side, temporary file address.
tempFilePaths?: string[];
// Exclusive to the web side, File object
files: File[];
};

Function return value type:

boolean | File[] | string[]
  • Returns true to continue the default upload logic.
  • Returns false to disable the default upload logic.
  • The web side returns a File array. The web side receives the file[] returned by the function as upload objects to complete the upload logic.
  • The Mini Program side returns a string[] temporary file address array. The Mini Program side receives the temporary file address array returned by the function as upload objects to complete the upload logic.

The application runtime can determine via the Platform API to refine multi-end custom upload logic.

Example: Custom Upload

For example, to customize uploading to another location such as COS. Below, using CloudBase Cloud Storage within the WeDa environment as an example:

async (result) => {
console.log('beforeUpload', result);
// Can be processed before upload, such as compression
// result.files[0] = FAKE_COMPRESS(result.files[0])

const tcb = await $w.cloud.getCloudInstance();
const res = await tcb.uploadFile({
cloudPath: 'ttt/abc.jpg',
filePath: result.files[0]
});

console.log(res.download_url); // Image HTTP address
$w.uploadImage1.setValue({ value: [res.download_url] }); // Set the obtained http address to the image upload component
return false; // Abort the upload
};

Events

Events exposed by the component. You can listen to component events to trigger external actions

Event Name
Event Code
Event Output Parameters event.detail
Applicable Scenarios
Description
值改变changeobject
  • value:
  • isDelete: boolean是否删除动作
Compatible with all platforms

用户修改组件值时触发

上传成功successobject
  • value:

    上传文件的fileId

  • file:

    上传文件file对象

Compatible with all platforms

-

上传失败errorCompatible with all platforms

-

Property API

Through the Property API, you can access the internal state and property values of components. You can access internal values using$w.componentId.propertyName, such as $w.input1.value. For details, please refer to Property API

Read-only Property Name
Property Identifier
Type
Description
绑定字段namestring

表单字段的Key值,用于提交数据时,匹配数据模型字段标识。表单内需保证唯一。

标题内容labelstring
文件值valuearray

支持以数组格式写入文件的https地址或cloudID;上传单个时支持字符串类型,上传多个时支持数组类型

必填requiredboolean
是否展示visibleboolean

组件是否展示

是否禁用disabledboolean

组件是否禁用

是否只读readOnlyboolean

组件是否只读

Method API

Through the Method API, you can programmatically trigger internal methods of components, such as submitting forms, displaying popups, etc. You can call component methods using $w.componentId.methodName, such as $w.form1.submit()

Method Name
Method Identifier
Parameters
Method Description
设置值setValueobject
  • value:
  • isDelete: boolean是否删除动作

通过 $w.id1.setValue("weda") 设置组件值

设置显隐setVisibleboolean显示

通过 $w.id1.setVisible(false) 设置组件为隐藏

设置禁用setDisabledboolean禁用

通过 $w.id1.setDisabled(true) 设置组件为禁用

清空值clearValue

通过 $w.id1.clearValue() 清空组件值

设置只读setReadOnlyboolean只读

通过 $w.id1.setReadOnly(true) 设置组件为只读

触发校验handleValidate

通过 $w.id1.handleValidate() 校验组件值

清除校验clearValidate

通过 $w.id1.clearValidate() 清除组件校验

Style API

Through the Style API, you can override the styles of internal elements in components to achieve customization. For example, in the low-code editor, you can write styles for all button components using #wd-page-root .wd-btn, and control individual component styles with :scope. For detailed instructions, please refer toStyle API

Name
Class Name
Description and Examples
根元素.wd-upload-file-root组件最外层元素
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-upload-file-root {
  /* Write CSS styles here */
}
H5 端根元素.wd-h5-upload-file-root可设定 H5 端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-h5-upload-file-root {
  /* Write CSS styles here */
}
PC 端根元素.wd-pc-upload-file-root可设定 PC 端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-pc-upload-file-root {
  /* Write CSS styles here */
}
小程序端根元素.wd-mp-upload-file-root可设定小程序端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-mp-upload-file-root {
  /* Write CSS styles here */
}
组件标题样式.wd-upload-file-root .wd-form-item-wrap__label组件标题元素

:scope .wd-form-item-wrap__label {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
表单控件根节点样式.wd-upload-file-root .wd-form-item-wrap__control设置表单控件根元素样式

:scope .wd-form-item-wrap__control {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
编辑态-校验信息.wd-upload-file-root .wd-g-text-error设置组件校验信息样式

:scope .wd-g-text-error {
    font-size: 20px;
    color: gray;
  }
提示文字.wd-upload-file-root .wd-form-item__help-text设置组件提示文字样式

:scope .wd-form-item__help-text {
    font-size: 20px;
    color: gray;
  }

Learn about Style API

Version Changes

  • Property changes
  • Style changes
  • widget api changes

Frequently Asked Questions

Is there a limit on the file size for uploads?

The size limit for file uploads primarily depends on the limitations of the underlying cloud storage. Currently, the PUT Object interface of the underlying cloud storage supports uploading files of up to 5GB. Although there is no strict limit, excessively large files tend to cause upload failures because the feature for resumable uploads is not yet supported. In practical applications, please select an appropriate file size for upload based on actual requirements and network conditions.

What file formats are supported for Mini Program file downloads?

On the Mini Program side, file preview and download are implemented via the wx.openDocument interface, which only supports downloading files in specific formats.

image