Advertisement
Guest User

FileUpload.jsx

a guest
Aug 19th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Meteor } from 'meteor/meteor';
  2. import React, { Component } from 'react';
  3.  
  4. import { createContainer } from 'meteor/react-meteor-data';
  5.  
  6. import FileIndividual from './FileIndividual.jsx';
  7. import { _ } from 'meteor/underscore';
  8.  
  9. export class FileUpload extends Component {
  10.  
  11.     state = {
  12.       uploading: [],
  13.       progress: 0,
  14.       inProgress: false
  15.     }
  16.  
  17.     uploadIt = (e) => {
  18.       e.preventDefault();
  19.  
  20.       var self = this;
  21.  
  22.       console.log("THIS:"+this);
  23.       console.log(self);
  24.  
  25.       if (e.currentTarget.files && e.currentTarget.files[0]) {
  26.         // We upload only one file, in case
  27.         // there was multiple files selected
  28.         var file = e.currentTarget.files[0];
  29.  
  30.         if (file) {
  31.           let uploadInstance = Images.insert({
  32.             file: file,
  33.             meta: {
  34.             //  locator: self.props.fileLocator,
  35.               userId: Meteor.userId() // Optional, used to check on server for file tampering
  36.             },
  37.             streams: 'dynamic',
  38.             chunkSize: 'dynamic',
  39.             allowWebWorkers: true // If you see issues with uploads, change self to false
  40.           }, false);
  41.  
  42.           self.setState({
  43.             uploading: uploadInstance, // Keep track of self instance to use below
  44.             inProgress: true // Show the progress bar now
  45.           });
  46.  
  47.           // These are the event functions, don't need most of them, it shows where we are in the process
  48.           uploadInstance.on('start', function () {
  49.             console.log('Starting');
  50.           });
  51.  
  52.           uploadInstance.on('end', function (error, fileObj) {
  53.             console.log('On end File Object: ', fileObj);
  54.           });
  55.  
  56.           uploadInstance.on('uploaded', function (error, fileObj) {
  57.             console.log('uploaded: ', fileObj);
  58.  
  59.             // Remove the filename from the upload box
  60.             self.refs['fileinput'].value = '';
  61.  
  62.             // Reset our state for the next file
  63.             self.setState({
  64.               uploading: [],
  65.               progress: 0,
  66.               inProgress: false
  67.             });
  68.           });
  69.  
  70.           uploadInstance.on('error', function (error, fileObj) {
  71.             console.log('Error during upload: ' + error);
  72.           });
  73.  
  74.           uploadInstance.on('progress', function (progress, fileObj) {
  75.             console.log('Upload Percentage: ' + progress);
  76.             // Update our progress bar
  77.             self.setState({
  78.               progress: progress
  79.             })
  80.           });
  81.  
  82.           uploadInstance.start(); // Must manually start the upload
  83.         }
  84.       }
  85.     }
  86.  
  87.  
  88.   // This is our progress bar, bootstrap styled
  89.   // Remove this function if not needed
  90.   showUploads = () => {
  91.     console.log('**********************************', this.state.uploading);
  92.  
  93.     if (!_.isEmpty(this.state.uploading)) {
  94.       return <div>
  95.         {this.state.uploading.file.name}
  96.  
  97.         <div className="progress progress-bar-default">
  98.           <div style={{width: this.state.progress + '%'}} aria-valuemax="100"
  99.              aria-valuemin="0"
  100.              aria-valuenow={this.state.progress || 0} role="progressbar"
  101.              className="progress-bar">
  102.             <span className="sr-only">{this.state.progress}% Complete (success)</span>
  103.             <span>{this.state.progress}%</span>
  104.           </div>
  105.         </div>
  106.       </div>
  107.     }
  108.   }
  109.  
  110.   render() {
  111.     if (this.props.docsReadyYet) {
  112.       'use strict';
  113.  
  114.       let fileCursors = this.props.docs;
  115.  
  116.       // Run through each file that the user has stored
  117.       // (make sure the subscription only sends files owned by this user)
  118.       let display = fileCursors.map((aFile, key) => {
  119.         // console.log('A file: ', aFile.link(), aFile.get('name'));
  120.         let link = Images.findOne({_id: aFile._id}).link();  //The "view/download" link
  121.  
  122.         // Send out components that show details of each file
  123.         return <div key={'file' + key}>
  124.           <IndividualFile
  125.             fileName={aFile.name}
  126.             fileUrl={link}
  127.             fileId={aFile._id}
  128.             fileSize={aFile.size}
  129.           />
  130.         </div>
  131.       });
  132.  
  133.       return <div>
  134.         <div className="row">
  135.           <div className="col-md-12">
  136.             <p>Upload New File:</p>
  137.             <input type="file" id="fileinput" disabled={this.state.inProgress} ref="fileinput"
  138.                  onChange={this.uploadIt}/>
  139.           </div>
  140.         </div>
  141.  
  142.         <div className="row m-t-sm m-b-sm">
  143.           <div className="col-md-6">
  144.  
  145.             {this.showUploads()}
  146.  
  147.           </div>
  148.           <div className="col-md-6">
  149.           </div>
  150.         </div>
  151.  
  152.         {display}
  153.  
  154.       </div>
  155.     }
  156.     else return <div></div>
  157.   }
  158. }
  159.  
  160. export default createContainer(() => {
  161.   var handle = Meteor.subscribe('files.images.all');
  162.  
  163.   return {
  164.     docsReadyYet: handle.ready(),
  165.     docs: Images.find().fetch()// Collection is Images
  166.   };
  167. }, FileUpload);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement