From 1b3ff01b47b45f646323c10d39586193f240ecc4 Mon Sep 17 00:00:00 2001 From: drewandrew Date: Thu, 20 Apr 2017 17:47:08 -0400 Subject: [PATCH] adds the ability to return both base64 strings and an HTML5 FileList objects --- README.md | 15 ++++++++++++++- ReactFileReader.js | 31 +++++++++++++++++-------------- index.js | 14 ++++++++------ package.json | 2 +- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 82484ab..888333e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ npm install react-file-reader --save ``` ## ChangeLog + - 1.1.0 + - adds the ability to return both base64 strings and an HTML5 FileList from handleFiles - 1.0.3 - bumps React version to 15.5 and fixes UNMET peer dependency with webpack - 1.0.2 @@ -69,6 +71,10 @@ handleFiles = files => { ### Base64 ```javascript +handleFiles = (files) => { + console.log(files.base64) +} + @@ -85,5 +91,12 @@ handleFiles = files => { "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..." ``` +###### Access HTML5 FileList with base64={true} +``` +handleFiles = (files) => { + console.log(files.fileList) +} +``` + ## Copyright -Copyright (c)2017 [Grillwork Inc](http://grillwork.io). See [LICENSE](https://github.com/GrillWork/react-file-reader/blob/master/LICENSE) for details. +Copyright (c)2017 [Grillwork Inc](http://grillwork.surge.sh). See [LICENSE](https://github.com/GrillWork/react-file-reader/blob/master/LICENSE) for details. diff --git a/ReactFileReader.js b/ReactFileReader.js index 3fb854c..5e9c092 100644 --- a/ReactFileReader.js +++ b/ReactFileReader.js @@ -5,7 +5,7 @@ import PropTypes from 'prop-types'; export default class ReactFileReader extends React.Component { state = { - elementId: this.props.elementId || uuidV4(), + elementId: this.props.elementId || uuidV4() } clickInput = () => { @@ -16,25 +16,26 @@ export default class ReactFileReader extends React.Component { handleFiles = (event) => { if(this.props.base64) { - this.convertFilesToBase64(event.target.files) + this.convertFilesToBase64(event.target.files); } else { - this.props.handleFiles(event.target.files) + this.props.handleFiles(event.target.files); } } convertFilesToBase64 = (files) => { - let ef = files + let ef = files; if (this.props.multipleFiles) { - let files = []; + let files = { base64: [], fileList: ef }; + for (var i = 0, len = ef.length; i < len; i++) { let reader = new FileReader(); let f = ef[i]; reader.onloadend = e => { - files.push(reader.result) + files.base64.push(reader.result); - if (files.length === ef.length) { + if (files.base64.length === ef.length) { this.props.handleFiles(files); } } @@ -42,14 +43,16 @@ export default class ReactFileReader extends React.Component { reader.readAsDataURL(f); } } else { - let f = files[0]; + let files = { base64: '', fileList: ef }; + let f = ef[0]; let reader = new FileReader(); - reader.onloadend = function (e) { - this.props.handleFiles(reader.result) - }.bind(this) + reader.onloadend = e => { + files.base64 = reader.result; + this.props.handleFiles(files); + } - reader.readAsDataURL(f) + reader.readAsDataURL(f); } } @@ -58,7 +61,7 @@ export default class ReactFileReader extends React.Component { width: '0px', opacity: '0px', position: 'fixed', - left: '-99999999px', + left: '-99999999px' } return( @@ -92,4 +95,4 @@ ReactFileReader.propTypes = { fileTypes: PropTypes.string, base64: PropTypes.bool, children: PropTypes.element.isRequired -} +}; diff --git a/index.js b/index.js index c27a1a9..850dce6 100644 --- a/index.js +++ b/index.js @@ -59,16 +59,16 @@ var ReactFileReader = function (_React$Component) { var i, len; (function () { - var files = []; + var files = { base64: [], fileList: ef }; var _loop = function _loop() { var reader = new FileReader(); var f = ef[i]; reader.onloadend = function (e) { - files.push(reader.result); + files.base64.push(reader.result); - if (files.length === ef.length) { + if (files.base64.length === ef.length) { _this.props.handleFiles(files); } }; @@ -81,12 +81,14 @@ var ReactFileReader = function (_React$Component) { } })(); } else { - var f = files[0]; + var _files = { base64: '', fileList: ef }; + var f = ef[0]; var _reader = new FileReader(); _reader.onloadend = function (e) { - this.props.handleFiles(_reader.result); - }.bind(_this); + _files.base64 = _reader.result; + _this.props.handleFiles(_files); + }; _reader.readAsDataURL(f); } diff --git a/package.json b/package.json index a9a1f45..6f251de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-file-reader", - "version": "1.0.3", + "version": "1.1.0", "description": "A flexible ReactJS component for handling styled HTML file inputs.", "main": "index.js", "repository": "git@github.com:GrillWork/react-file-reader.git",