Skip to content

Upload

A file upload button with a simple upload wrapper.

Basic Usage

Basic usage: click to upload, selecting one file at a time.

<template>
  <div style="width: 400px">
    <b-upload action="//jsonplaceholder.typicode.com/posts/"></b-upload>
  </div>
</template>

Multi-select

Enable multi-select mode to select multiple files at once.

<template>
  <div style="width: 400px">
    <b-upload action="//jsonplaceholder.typicode.com/posts/" multiple>
      <b-button icon="cloud-upload" plain type="primary">Click to upload</b-button>
    </b-upload>
  </div>
</template>

Manual Upload

Bind before-upload and return false to prevent the default upload process, allowing manual control of file upload.

You can freely control the upload logic. Refer to the API implementation for details.

<template>
  <div style="width: 400px">
    <b-upload
      action="//jsonplaceholder.typicode.com/posts/"
      :before-upload="handleUpload"
    ></b-upload>

    <div v-if="file !== null" flex="cross:center">
      Upload file: {{ file.name }}
      <b-button type="text" :loading="loadingStatus" @click="upload">
        {{ loadingStatus ? 'Uploading...' : 'Click to upload' }}
      </b-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Message } from 'bin-ui-design'

const file = ref(null)
const loadingStatus = ref(false)

function handleUpload(f) {
  file.value = f
  return false
}
function upload() {
  loadingStatus.value = true
  setTimeout(() => {
    file.value = null
    loadingStatus.value = false
    Message({ type: 'success', message: '上传Success' })
  }, 1500)
}
</script>

Drag and Drop Upload

Set the type property to drag to enable drag-and-drop upload.

Click or drag files here to upload

<template>
  <div style="width: 400px">
    <b-upload action="//jsonplaceholder.typicode.com/posts/" multiple type="drag">
      <div
        style="padding: 20px 0; border: 1px dashed #eee; cursor: pointer"
        flex="dir:top main:center cross:center"
      >
        <p>
          <b-icon name="cloud-upload" size="52" style="color: #3399ff"></b-icon>
        </p>
        <p>Click or drag files here to upload</p>
      </div>
    </b-upload>
  </div>
</template>

<script setup lang="ts"></script>

Attributes

ParameterDescriptionTypeOptionsDefault
actionUpload URL. Requiredstring
headersUpload request headersObject{}
multipleWhether to support multi-selectBooleanfalse
pasteWhether to support paste uploadBooleanfalse
disabledDisabledBooleanfalse
dataAdditional parameters sent with the uploadObject
nameUpload file field nameStringfile
with-credentialsSupport sending cookie credentialsBooleanfalse
show-upload-listWhether to show the uploaded file listBooleantrue
typeUpload control typeStringselect / dragselect
acceptAccepted file types. Native accept attribute of input. Filters files on selectionString
formatSupported file types. Unlike accept, format identifies files by their extensionString
max-sizeFile size limit in KBNumber
before-uploadHook before upload. Receives the file. If it returns false or a rejected Promise, the upload is cancelledFunction
on-progressHook during upload. Returns event, file, fileListFunction
on-successHook on upload success. Returns response, file, fileListFunction
on-errorHook on upload error. Returns error, file, fileListFunction
on-previewHook when clicking an uploaded file link. Returns file. Access server response via file.responseFunction
on-removeHook when a file is removed from the list. Returns file, fileListFunction
on-format-errorHook on file format validation failure. Returns file, fileListFunction
on-exceeded-sizeHook when file exceeds size limit. Returns file, fileListFunction
default-file-listDefault uploaded file list, e.g., [{ name: 'img1.jpg', url: 'http://www.xxx.com/img1.jpg' }, { name: 'img2.jpg', url: 'http://www.xxx.com/img2.jpg' }]Array[]

Methods

Method NameDescriptionParameter
clearFilesClear the uploaded file list

Slot

NameDescription
defaultThe trigger element for the upload component. Defaults to a button
tipHelper tip message, e.g., "A single file cannot exceed 2MB"