2.2.1 • Published 4 months ago

vue3-dropzone v2.2.1

Weekly downloads
386
License
ISC
Repository
github
Last release
4 months ago

vue3-dropzone

It's inspired by react-dropzone and implemented with vue3.

Installation

npm install --save vue3-dropzone

or

yarn add vue3-dropzone

Usage

Basic use with flexibility. acceptFiles is an array returned in the same format as FileList where all the dropped files are turned into a File class before saving to the array.

<template>
  <div>
    <div v-bind="getRootProps()">
      <input v-bind="getInputProps()" />
      <p v-if="isDragActive">Drop the files here ...</p>
      <p v-else>Drag 'n' drop some files here, or click to select files</p>
    </div>
    <button @click="open">open</button>
  </div>
</template>

<script>
import { useDropzone } from "vue3-dropzone";

export default {
  name: "UseDropzoneDemo",
  setup() {
    function onDrop(acceptFiles, rejectReasons) {
      console.log(acceptFiles);
      console.log(rejectReasons);
    }

    const { getRootProps, getInputProps, ...rest } = useDropzone({ onDrop });

    return {
      getRootProps,
      getInputProps,
      ...rest,
    };
  },
};
</script>

Save multiple files

Save multiple files through axios requests and FormData. You will need a backend to loop through the received files and save them individually in the loop.

<template>
  <div>
    <div v-bind="getRootProps()">
      <input v-bind="getInputProps()" />
      <p v-if="isDragActive">Drop the files here ...</p>
      <p v-else>Drag 'n' drop some files here, or click to select files</p>
    </div>
    <button @click="open">open</button>
  </div>
</template>

<script>
import { useDropzone } from "vue3-dropzone";
import axios from "axios";

export default {
  name: "UseDropzoneDemo",
  setup() {
    const url = "{your_url}"; // Your url on the server side
    const saveFiles = (files) => {
      const formData = new FormData(); // pass data as a form
      for (var x = 0; x < files.length; x++) {
        // append files as array to the form, feel free to change the array name
        formData.append("images[]", files[x]);
      }

      // post the formData to your backend where storage is processed. In the backend, you will need to loop through the array and save each file through the loop.

      axios
        .post(url, formData, {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        })
        .then((response) => {
          console.info(response.data);
        })
        .catch((err) => {
          console.error(err);
        });
    };

    function onDrop(acceptFiles, rejectReasons) {
      saveFiles(acceptFiles); // saveFiles as callback
      console.log(rejectReasons);
    }

    const { getRootProps, getInputProps, ...rest } = useDropzone({ onDrop });

    return {
      getRootProps,
      getInputProps,
      ...rest,
    };
  },
};
</script>

☕️

Could you buy me a coffee, if my effort save your time?

微信(Wechat)支付宝(alipay)

API

const result = useDropzone(options)

options

propertytypedescription
onDropFunctionCb for when the drop event occurs. Note that this callback is invoked after the getFilesFromEvent callback is done.
acceptString / Array<String>Set accepted file types. See https://github.com/okonet/attr-accept for more information.
disabledBooleanEnable/disable the dropzone
maxSizeNumberMaximum file size (in bytes)
minSizeNumberMinimum file size (in bytes)
multipleNumberAllow of multiple files
maxFilesNumberMaximum accepted number of files The default value is 0 which means there is no limitation to how many files are accepted
getFilesFromEventFunctionUse this to provide a custom file aggregator
onDragenterFunctionCb for when the dragenter event occurs.
onDragoverFunctionCb for when the dragover event occurs
onDragleaveFunctionCb for when the dragleave event occurs
onDropAcceptedFunctionCb for when the drop event occurs. Note that if no files are accepted, this callback is not invoked.
onDropRejectedFunctionCb for when the drop event occurs. Note that if no files are rejected, this callback is not invoked.
onFileDialogCancelFunctionCb for when closing the file dialog with no selection
preventDropOnDocumentBooleanIf false, allow dropped items to take over the current browser window
noClickBooleanIf true, disables click to open the native file selection dialog
noKeyboardBooleanIf true, disables SPACE/ENTER to open the native file selection dialog. Note that it also stops tracking the focus state.
noDragBooleanIf true, disables drag 'n' drop
noDragEventsBubblingBooleanIf true, stops drag event propagation to parents

result

propertytypedescription
isFocusedRef<Boolean>
isFileDialogActiveRef<Boolean>
isDragActiveRef<Boolean>
isDragAcceptRef<Boolean>
isDragRejectRef<Boolean>
draggedFilesRef<Array>dragged files
acceptedFilesRef<Array>accepted files
fileRejectionsRef<Array>files rejections
getRootPropsFunctionFunction to generate element props which contains input
getInputPropsFunctionFunction to generate input props
rootRefRef<HTMLElement>ref a dom element
inputRefRef<HTMLElement>ref a input element
openFunctionOpen file selection dialog

Run example

cd examples
yarn install
yarn dev
2.2.1

4 months ago

2.2.0

5 months ago

2.1.2

8 months ago

2.1.1

8 months ago

2.1.0

9 months ago

2.0.1

2 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

1.0.1

3 years ago