본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Uploader — 파일 업로더

원문 보기 위키 갱신

로컬 사진이나 파일을 서버로 올릴 때 쓰는 업로드 컴포넌트예요. 업로드 상태 표시, 진행률, 개수·용량 제한, 업로드 전 처리(hook)까지 파일 업로드에 필요한 기능을 대부분 갖추고 있어요.

출처: Uploader — doc.en-US.md

본문

로컬 사진이나 파일을 서버로 업로드할 때 사용해요.

설치

import { createApp } from 'vue'
import { Uploader } from '@nutui/nutui'

const app = createApp()
app.use(Uploader)

기본 사용법

:::demo

<template>
  <nut-uploader url="https://xxxx"></nut-uploader>
</template>

:::

업로드 상태

:::demo

<template>
  <nut-uploader :url="uploadUrl" v-model:file-list="defaultFileList" maximum="3" multiple></nut-uploader>
</template>
<script setup>
import { reactive } from 'vue'
const uploadUrl = 'https://xxxxx'
const defaultFileList = reactive([
  {
    name: 'file 1.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'success',
    message: 'upload success',
    type: 'image'
  },
  {
    name: 'file 2.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'error',
    message: 'upload error',
    type: 'image'
  },
  {
    name: 'file 3.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'uploading',
    message: 'uploading...',
    type: 'image'
  }
])
</script>

:::

기본 사용법 - 업로드 목록 표시

:::demo

<template>
  <nut-uploader :url="uploadUrl" v-model:file-list="defaultFileList" maximum="10" multiple list-type="list">
    <nut-button type="success" size="small">Upload files</nut-button>
  </nut-uploader>
</template>
<script setup>
import { reactive } from 'vue'
const uploadUrl = 'https://xxxxx'
const defaultFileList = reactive([
  {
    name: 'file 1.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'success',
    message: 'upload success',
    type: 'image'
  },
  {
    name: 'file 2.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'error',
    message: 'upload error',
    type: 'image'
  },
  {
    name: 'file 3.png',
    url: 'https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif',
    status: 'uploading',
    message: 'uploading...',
    type: 'image'
  }
])
</script>

:::

업로드 스타일 커스터마이즈

:::demo

<template>
  <nut-uploader url="https://xxxx">
    <nut-button type="success" size="small">Upload files</nut-button>
  </nut-uploader>
</template>

:::

커스텀 업로드에서 기본 진행률 바 사용

:::demo

<template>
  <nut-uploader :url="uploadUrl" @progress="onProgress">
    <nut-button type="success" size="small">Upload files</nut-button>
  </nut-uploader>
  <br />
  <nut-progress
    :percentage="progressPercentage"
    stroke-color="linear-gradient(270deg, rgba(18,126,255,1) 0%,rgba(32,147,255,1) 32.815625%,rgba(13,242,204,1) 100%)"
    :status="progressPercentage == 100 ? '' : 'active'"
  >
  </nut-progress>
</template>
<script setup>
import { ref } from 'vue'
import { showToast } from '@nutui/nutui'
const uploadUrl = 'https://xxxxx'
const progressPercentage = (ref < string) | (number > 0)
const onProgress = ({ event, options, percentage }) => {
  progressPercentage.value = percentage
  showToast.text('progress 事件触发' + percentage)
}
</script>

:::

카메라 바로 띄우기 (모바일에서 유효)

:::demo

<template>
  <nut-uploader url="https://xxxx" capture></nut-uploader>
</template>

:::

업로드 개수 5개로 제한

:::demo

<template>
  <nut-uploader url="https://xxxx" multiple maximum="5"></nut-uploader>
</template>

:::

업로드 용량 제한 (파일당 최대 50kb)

:::demo

<template>
  <nut-uploader :url="uploadUrl" multiple :maximize="1024 * 50" @oversize="onOversize"></nut-uploader>
</template>
<script setup>
import { ref } from 'vue'
import { showToast } from '@nutui/nutui'
const uploadUrl = 'https://xxxxx'
const onOversize = (files) => {
  showToast.text('oversize limit upload size (up to 50kb per file)')
}
</script>

:::

이미지 압축 (beforeupload 훅에서 처리)

:::demo

<template>
  <nut-uploader :url="uploadUrl" multiple :before-upload="beforeUpload"></nut-uploader>
</template>
<script setup>
import { ref } from 'vue'
const uploadUrl = 'https://xxxxx'
const fileToDataURL = (file) => {
  return new Promise((resolve) => {
    const reader = new FileReader()
    reader.onloadend = (e) => resolve(e.target.result)
    reader.readAsDataURL(file)
  })
}
const dataURLToImage = (dataURL) => {
  return new Promise((resolve) => {
    const img = new Image()
    img.onload = () => resolve(img)
    img.src = dataURL
  })
}
const canvastoFile = (canvas, type, quality) => {
  return new Promise((resolve) => canvas.toBlob((blob) => resolve(blob), type, quality))
}
const beforeUpload = async (file) => {
  let fileName = file[0].name
  const canvas = document.createElement('canvas')
  const context = canvas.getContext('2d')
  const base64 = await fileToDataURL(file[0])
  const img = await dataURLToImage(base64)
  canvas.width = img.width
  canvas.height = img.height

  context.clearRect(0, 0, img.width, img.height)
  context.drawImage(img, 0, 0, img.width, img.height)

  let blob = await canvastoFile(canvas, 'image/jpeg', 0.5) //quality:0.5可根据实际情况计算
  const f = await new File([blob], fileName)
  return [f]
}
</script>

:::

FormData · headers 커스터마이즈

:::demo

<template>
  <nut-uploader :url="uploadUrl" :data="formData" :headers="formData" :with-credentials="true"></nut-uploader>
</template>
<script setup>
import { ref } from 'vue'
const uploadUrl = 'https://xxxxx'
const formData = {
  custom: 'test'
}
</script>

:::

XHR 업로드 커스터마이즈(before-xhr-upload)

:::demo

<!-- When the upload method is put, upload the source file stream directly -->
<template>
  <nut-uploader url="https://xxxx" method="put" :before-xhr-upload="beforeXhrUpload"></nut-uploader>
</template>

<script setup>
import { ref } from 'vue'
// source file https://github.com/jd-opensource/nutui/blob/v4/src/packages/__VUE/uploader/uploader.ts#L51
const beforeXhrUpload = (xhr, options) => {
  if (options.method.toLowerCase() == 'put') {
    xhr.send(options.sourceFile)
  } else {
    xhr.send(options.formData)
  }
}
</script>

:::

파일 선택 후 버튼으로 업로드 실행

:::demo

<template>
  <nut-uploader :url="uploadUrl" maximum="5" :auto-upload="false" ref="uploadRef"></nut-uploader>
  <br />
  <nut-button type="success" size="small" @click="submitUpload">Perform upload</nut-button>
  <nut-button type="danger" size="small" @click="clearUpload">Clear upload</nut-button>
</template>
<script setup>
import { ref } from 'vue'
const uploadUrl = 'https://xxxxx'
const uploadRef = ref(null)
const submitUpload = () => {
  uploadRef.value.submit()
}
const clearUpload = () => {
  uploadRef.value.clearUploadQueue()
}
</script>

:::

비활성 상태

:::demo

<template>
  <nut-uploader disabled></nut-uploader>
</template>

:::

API

Props

Attribute Description Type Default
auto-upload Whether to upload the file immediately after selecting it, if false, you need to manually execute the ref submit method to upload boolean true
name The name of the input tag name, the file parameter name sent to the background string "file"
url The interface address of the upload server string -
v-model:file-list List of uploaded files by default FileItem[] []
is-preview Whether to display the preview image after the upload is successful boolean true
is-deletable Whether to display the delete button boolean true
method The http method of upload request string "post"
list-type Built-in stylesheets, support for three types: picture、list string "picture"
capture Capture, can be set to camera,turn on the camera directly string false
maximize You can set the maximum upload file size (bytes) number | string Number.MAX_VALUE
maximum File upload limit number | string 1
clear-input Whether to clear the input content, set to true to support repeated selection and upload of the same file boolean true
accept File types that can be accepted. See input accept Attribute string *
headers Set request headers object {}
data Uploading extra params or function which can return uploading extra params object {}
upload-icon-size Upload area [icon size] (#/en-US/component/icon) size, such as 20px 2em 2rem string | object -
xhr-state The success status (status) value of the interface response number 200
with-credentials The ajax upload with cookie sent boolean false
multiple Whether to support multiple file selection boolean false
disabled Whether to disable file upload boolean false
timeout timeout, in milliseconds number | string 1000 * 30
before-upload Hook before reading the file, return false to stop reading the file, can return Promise Function null
before-xhr-upload Customize the method when uploading XHR Function(xhr,option) null
before-delete Hook before delete the file, return false to stop reading the file, can return Promise Function(file,fileList): boolean 丨Promise -

FileItem

Attribute Description Default
status File status value, optional ready,uploading,success,error ready
uid Unique ID of the file new Date().getTime().toString()
name File name -
url File path -
type File type "image/jpeg"
formData Upload the required data new FormData()
percentage Upload percentage 0

Events

Event Description Arguments
start File upload starts options
progress The progress of the file upload {event,option,percentage}
oversize Triggered when the file size exceeds the limit files
success Uploaded successfully {responseText,option,fileItem}
failure Upload failed {responseText,option,fileItem}
change The state when the uploaded file changes {fileList,event}
delete File delete event {files,fileList,index}
file-item-click Click to trigger after the file is uploaded successfully {fileItem}

Uploader Slots

Name Description
default Default slot
upload-icon Custom Uploader Button Center icon
delete-icon Custom Right Delete Button

Methods

ref로 Uploader 인스턴스를 받아서 인스턴스 메서드를 호출할 수 있어요.

Name Description Arguments Return value
submit Manual upload mode, perform upload operation - -
clearUploadQueue Empty the selected file queue (this method is generally used when uploading in manual mode) index -

테마 (Theming)

CSS 변수

이 컴포넌트는 스타일을 커스터마이징할 때 쓸 수 있는 CSS 변수를 제공해요. 자세한 내용은 ConfigProvider 컴포넌트 문서를 참고하세요.

Name Default Value
--nut-uploader-picture-width 100px
--nut-uploader-picture-height 100px
--nut-uploader-background #f7f8fa

더 알아보기 (Learn more)

이 문서는 NutUI v4 공식 영문 문서를 한국어로 옮긴 글이에요. 최신 API와 변경 사항은 원문에서 확인하는 게 가장 정확해요.