1.1.2 • Published 3 years ago

v-file-picker v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

VFilePicker Plugin for Vue 3

A simple Vue 3 file picker including basic file validation. Such as file size and file type validation.

Install Plugin

npm i v-file-picker

How to use !!!

Method 1

Import within a specific component to use only in a component

After installing import the component in your desired file.

import { VFilePicker } from "v-file-picker";

export default {
  name: "YourComponentName",
  data() {
    return {
      document: "",
    };
  },
};
<!-- Component.vue template -->
<template>
  <v-file-picker
    @fileUploaded="(file) => document = file"
    name="document"
    id="docFile"
    class="col-md-6"
  >
    Document
    <!-- Input label -->
  </v-file-picker>
</template>

Method 2

Import in main.js file to use globally

After installing import the component in your main.js file.

import VFilePicker from "v-file-picker";

createApp(App).use(VFilePicker).mount("#app");
<!-- Component.vue template -->
<template>
  <v-file-picker
    @fileUploaded="(file) => document = file"
    name="document"
    id="docFile"
    class="col-md-6"
  >
    Document
    <!-- Input label -->
  </v-file-picker>
</template>

Attention:

VFilePicker is using bootstrap 5 CSS file. If you have already included a bootstrap CSS file in your project you don't need to import the VFilePicker CSS file. Otherwise, you need to import the CSS file.

<!-- Import css file -->
<style>
  @import "v-file-picker/dist/style.css";
</style>

Example 1

VFilePickerFileEXtValidation

import VFilePicker from "v-file-picker";

export default {
  name: "YourComponentName",
  data() {
    return {
      document: "",
      extensions: ["jpg", "png", "pdf", "doc"],
    };
  },
};
<template>
  <v-file-picker
    @fileUploaded="(file) => document = file"
    :validExtension="extensions"
    name="document"
    id="docFile"
    class="col-md-6"
  >
    Document
  </v-file-picker>
</template>

Example 2

VFilePickerFileSizeValidation

import VFilePicker from "v-file-picker";

export default {
  name: "YourComponentName",
  data() {
    return {
      document: "",
      extensions: ["jpg", "png", "pdf", "doc"],
    };
  },
};
<template>
  <v-file-picker
    @fileUploaded="(file) => document = file"
    :validExtension="extensions"
    :size="2097152"
    name="document"
    id="docFile"
    class="col-md-6"
  >
    <!-- Difine size in bytes -->
    Document
  </v-file-picker>
</template>

Example 3

VFilePickerFileSizeValidation

import VFilePicker from "v-file-picker";

export default {
  name: "YourComponentName",
  data() {
    return {
      document: "",
      extensions: ["jpg", "png", "pdf", "doc"],
      fileName: "https://webartist.xyz/with_file_name_and_url.png",
      fileURL: "with_file_name_and_url.png",
    };
  },
  methods: {
    removeDoc() {
      this.fileName = "";
      this.fileURL = "";
    },
  },
};
<template>
  <v-file-picker
    @fileUploaded="(file) => document = file"
    @removeDoc="removeDoc"
    :validExtension="extensions"
    :size="2097152"
    name="document"
    id="docFile"
    class="col-md-6"
  >
    <!-- Difine size in bytes -->
    Document
  </v-file-picker>
</template>

Configuration

Props

NameTypeDefaultDescription
sizeNumber2097152To validate the file size pass a number in bytes.
validExtensionarray['jpg', 'jpeg', 'png', 'svg', 'txt', 'xlx', 'pdf', 'docx', 'doc', 'csv']To validate the file type pass an Array of extensions.
extValidationbooleantrueUse to enable or disable file type validation.
sizeValidationbooleantrueUse to enable or disable file size validation.
extValidationMsgstringFile extension not supported!Use string to customize extension validation message.
sizeValidationMsgstringFile size is more than 2 MBUse string to customize file size validation message.
optionalbooleanfalseUse to show optional word beside the label if your field is optional.
errorMszstring''Use it if you want to show the user any custom error message.
idstring''Use it to set input id
fileUrlstring''Use a file URL to show the file that the user previously uploaded. We normally use this when we are updating data.
fileNamestring''Show the file name that the user previously uploaded. We normally use this when we are updating data.

Events

NameDescription
fileUploadedThis event will emit when the user uploads a file.
removeDocThis event will emit when the user presses the remove button to remove the previous file and upload a new one. Normally we need this event when a user tries to update data.