Giter Club home page Giter Club logo

drop-files's Introduction

npm version Maintainability Test Coverage Build Status codecov

Drop Files

Drop File is a simple react component that allows you select files from your computer.

With Drop File you can select multiple files from your computer or simply drag and drop them, and you also get an instant preview of the files selected from your computer.

Installation

run npm i dropfiles

Cloudinary Support

The awesome thing about this library is that developers who uses cloudinary for files upload can also upload their files to cloudinary stress-free.

Media Support

List of media files supported at the moment are :

  • jpg
  • jpeg
  • png
  • pdf
  • mp3
  • mp4
  • media urls
  • audio/video

Installation

  • npm i dropfiles

    create .env file, add your CLOUDINARY_UPLOAD_PRESET = your cloudinary preset and CLOUDINARY_UPLOAD_URL= your cloudinary upload url.

  • webpack configuration

    rules: [{
      test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)(\?\S*)?$/,
      loader: 'url-loader'
    }],
    node: {
      fs: "empty"
    }
  }

Uploading files to Cloudinary

To upload your files to cloudinary all you have to do is import the uploadToCloudinary function and have a state name files which should be an array, and call the uploadToCloudinary function and pass in the files as an argument. The uploadToCloudinary function returns a promise, the promise contains an array of urls for the files uploaded to cloudinary.

Note

If you are attaching any token to your request in your application please make sure you clear them before calling the uploadToCloudinary function, else you get an error from Cloudinary

Usage

import React, Component from 'react'
import DropFile, { uploadToCloudinary } from 'dropfiles'

/**
* @description Example Component
* 
* @className DropFile
* @extends {Component}
*/
class Example extends Component{
/**
 * @description Creates an instance of DropFile.
 *
 * @method constructor
 * 
 * @param {Object} props
 *
 * @memberof DropFile
 */
constructor(props){
  super(props);
  this.state = {
    files: [],
    showFilePreviewer: false,
  }
}

uploadToCloudinary = async (event) => {
  event.preventDefault()
  this.setState({ fileUploading: true })
  const { files } = this.state;
  Reflect.deleteProperty(axios.defaults.headers.common, 'x-access-token');
  const uploadedFiles = await uploadToCloudinary(files); // contains the urls of the uploaded files
  this.setState({ fileUploading: false, files: [], uploadedFiles })
}

/**
 * 
 * 
 * @returns {Jsx} Jsx
 * @memberof DropFile
 */
render(){
    return (
      <div>
                <button type="button" className="btn btn-primary" 
        data-toggle="modal" data-target="#exampleModalCenter"
      >
        Launch demo modal
      </button>

      <div className="modal fade"
        id="exampleModalCenter" tabIndex="-1" role="dialog" 
        aria-labelledby="exampleModalCenterTitle" aria-hidden="true"
      >
        <div className="modal-dialog modal-dialog-centered" role="document">
          <div className="modal-content">
            <div className="modal-header">
              <h5 className="modal-title" id="exampleModalLongTitle">
                Upload Files
              </h5>
              <button type="button" className="close" data-dismiss="modal" 
                aria-label="Close"
              >
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div className="modal-body">
              <form>
                <DropFile that={this}/>
                <button className = {
                  this.state.fileUploading ? 'fa fa-spinner' : '' 
                  }
                  disabled = {this.state.fileUploading}
                onClick= {this.uploadToCloudinary}
                >
                  Upload
                </button>
              </form>
            </div>
            <div className="modal-footer">
              <button type="button" className="btn btn-secondary"
               data-dismiss="modal"
              >
                Close
              </button>
              <button type="button" className="btn btn-primary"
              >
                Save changes
              </button>
            </div>
          </div>
        </div>
      </div>
      </div>
    );
  }
}


export default Example;

The Example Component is your own component, all you need to do is instantiate the Drop File Component and pass in the props that which is the this of the your own component and you're good to go. i.e <DropFile that={this} />

FAQ

  • Who can contribute ?

    Anyone

  • How do I contribute?

    Check the CONTRIBUTING.md file to see how to contribute. Thank you

Author


Seun Agbeye SEUNAGBEYE

License


This is licensed for your use, modification and distribution under the MIT LICENSE

drop-files's People

Contributors

seunagbeye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

midetech

drop-files's Issues

Ignore Dist folder in version control

The contents of the dist folder and the scr folder are the same thing. The dist folder should be a user-generated resource and should be ignored prior to pushing to version control

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.