GithubHelp home page GithubHelp logo

saimow / vue-media-upload Goto Github PK

View Code? Open in Web Editor NEW
198.0 4.0 35.0 288 KB

๐Ÿ–ผ๏ธ Vue 3 component that handle multiple images upload with preview. This package support the create and the update form.

License: MIT License

Vue 99.37% JavaScript 0.63%
vue webdev image-upload image-processing image-preview dropzone form image-uploader vue3 vuejs

vue-media-upload's Introduction

npm npm npm npm

vue-media-upload

๐Ÿ“ท vue-media-upload is a Vuejs package that handle multiple images upload and preview.

๐Ÿ–ผ๏ธ This package support the create and the update form, and handles the upload for you.

vue-media-upload - multiple image upload with preview

๐Ÿ‘€ Demo

๐Ÿ’ป Install

via npm

npm install vue-media-upload

or via yarn

yarn add vue-media-upload

๐Ÿ•น Usage

import { createApp } from 'vue';

import Uploader from 'vue-media-upload';

let app = createApp({})

app.component('Uploader', Uploader);

app.mount("#app")

or

import Uploader from "vue-media-upload";

export default {
  components: {
    Uploader
  }
};

๐Ÿ”Ž Example

Create Form

<template>
  <div>
    <Uploader
      server="/api/upload"
      @change="changeMedia"
    />
  </div>
</template>

<script>
  import Uploader from 'vue-media-upload'

  export default {
    data() {
      return {
        media: []
      }
    },
    methods:{
      changeMedia(media){
        this.media = media
      }
    },
    components: {
      Uploader
    },
  }
</script>

Update Form

<template>
  <div>
    <Uploader
      server="/api/upload"
      :media="media.saved"
      path="/storage/media"
      @add="addMedia"
      @remove="removeMedia"
    />
  </div>
</template>

<script>
  import Uploader from 'vue-media-upload'

  export default {
    data() {
      return {
        media: {
          saved: [
            { name: '123_image.jpg' },
            { name: '456_image.jpg' },
          ],
          added: [],
          removed: []
        }
      }
    },
    methods:{
      addMedia(addedImage, addedMedia){
        this.media.added = addedMedia
      },
      removeMedia(removedImage, removedMedia){
        this.media.removed = removedMedia
      }
    },
    components: {
      Uploader
    },
  }
</script>

โš™๏ธ Props

Prop Type Default Description
server String '/api/upload' The Route that handle the image upload. The Upload handler should return the name of the uploaded image in the following format:
{ "name": "123_image.jpg" }
isInvalid Boolean false Whether error styling should be applied.
media Array [] The list of the stored images, that each of which must have the property name containing the name of the image.
[ { name: '123_image.jpg' } , { name: '456_image.jpg' } ]
location String '' The location of the folder where the saved images are stored.
max Number null The maximum number of files allowed to be uploaded.
maxFilesize Number 4 The maximum filesize (in megabytes) that is allowed to be uploaded
warnings Boolean true By default, the package uses JavaScript alerts to display warnings. In case you want to use your custom warnings, you can disable the component pop-ups using this prop.
headers Object null An optional prop to send additional headers to the server. Eg: { "Authorization": "Bearer ACCESS_TOKEN" }

๐Ÿ’พ Events

Event Payload Description
@init param : The list of all the listed images. Emitted when the component is ready to use.
@change param : The list of all the listed images. Emitted after an image was added or removed.
@add param1 : The image that was added.
param2 : The list of the added Images.
Emitted after an image was added.
@remove param1 : The image that was removed.
param2 : The list of images that have been removed from the stored media.
Emitted after an image was removed.
@max Emitted when max prop is exceeded.
@max-filesize param : The image size. Emitted when maxFilesize prop is exceeded.

๐Ÿ“™ How it works in a Server-Rendered Form?

  1. vue-media-upload component uploads the image image.jpg as multipart/form-data using a POST request.

  2. server temporary saves the image with a unique name 123_image.jpg in a /tmp/uploads folder.

  3. server returns the unique name 123_image.jpg in a request response.

  4. vue-media-upload insert the unique name 123_image.jpg in a hidden html input with name="added_media[]".

  5. user submits the component parent form, which includes the hidden input field containing the unique image name.

  6. server uses the unique image name to move 123_image.jpg from the /tmp/uploads folder to its final location.

๐Ÿ”ฃ Inputs

Note that all this inputs are hidden and they are just a way to validate and pass data to the backend when using this package in a Server-Rendered Form!

Name attribute Description
added_media[] The added images in the component
removed_media[] The images that have been removed from the stored media.
media This input is added, when the component has at least one image or more listed, as a way for the backend to validate the Images as being required.

๐Ÿค Contributing

  1. Fork this repository.
  2. Create new branch with feature name.
  3. Create your feature.
  4. Commit and set commit message with feature name.
  5. Push your code to your fork repository.
  6. Create pull request. ๐Ÿ™‚

โญ๏ธ Support

If you like this project, You can support me with starring โญ this repository.

vue-media-upload - multiple image upload with preview

๐Ÿ“„ License

MIT

Developed with โค๏ธ

vue-media-upload's People

Contributors

saimow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vue-media-upload's Issues

Upload docs

I'm trying to upload docs as pdf and it works, but there is no thumbnail icon.
Is it possible to have a generic docs icon?
thanks and compliments!

Axios dependency

Hi there, cool library! I would love to use this in Vue 3, got it on display also but it complains about axios not being defined. I provided the full API path in the "server" tag. Do I have to initialize something in axios?

Wrong documentation emit events' name

Update-media's emit events' name is wrong in documentation.
@added_media should be @added-media,
@deleted_media should be @deleted-media,
@saved_media should be @saved-media,

Type declarations?

Can you include a .d.ts file in the component so that it could be imported in Typescript-based Vue 3 projects without requiring allowJS: false?

Vue2 warning: Invalid default value for prop "media"

Good day,
I get the following errors in console:

[Vue warn]: Invalid default value for prop "media": Props with type Object/Array must use a factory function to return the default value.

found in ---> <Uploader> at node_modules/vue-media-upload/src/Uploader.vue

It s there anything i m not doing fine?

Edit: I m using this on Vue2

Drag and drop

Hi. Is it possible to upload files on drag and drop?

No server error handling?

Hey, I think there should be an event so when the axios request fails, an event is triggered.

Something like:
const data = await axios.post(this.server, formData)
.catch( error => {
this.$emit('serverError', error)
});

                        if( data ){
                            let addedImage = {url:url, name:data.name, size:files[i].size, type:files[i].type}
                            this.addedMedia.push(addedImage)
    
                            this.$emit('change', this.allMedia)
                            this.$emit('add', addedImage, this.addedMedia)    
                        }

instead of only:
const {data} = await axios.post(this.server, formData)

What do you think?

Using multiple times

I tried to use in a form multiple times but it does not work.
I would need it for example if a model has a basic photo and other media collections. Even if I want to upload at the second one, it appears at the first one

   <div class="row g-4 mt-1">
          <Uploader server="/store-media" @change="changeBasicMedia"/>
        </div>
        <div class="row g-4 mt-1">
          <Uploader server="/store-media" @change="changeMediaCollection"/>
        </div>
const basicMedia = ref();
const mediaCollection = ref([]);

const changeBasicMedia = (basic) => {
  basicMedia.value = basic;
};

const changeMediaCollection = (collection) => {
  mediaCollection.value = collection;
};

Use it directly

Hello sir.

Is it possible to import it as a script file?

In one of my site I'am using vue as a script file without the use of webpack.

Something like this
<script src="https://unpkg.com/media-upload-component.js"></script>

Thanks!

Select single image

This is not an Issue. But how do we make it to select only a single file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.