import { withTracker } from 'meteor/react-meteor-data'; import { Meteor } from 'meteor/meteor'; import React, { Component } from 'react'; // import PropTypes from 'prop-types'; import { FilesCol } from '/imports/api/files.js'; import IndividualFile from '/imports/ui/files/IndividualFile.jsx'; import _ from 'lodash'; const debug = require('debug')('demo:file'); class FileUploadComponent extends Component { constructor(props) { super(props); this.state = { uploading: [], progress: 0, inProgress: false }; this.uploadIt = this.uploadIt.bind(this); } uploadIt(e) { e.preventDefault(); let self = this; if (e.currentTarget.files && e.currentTarget.files[0]) { // We upload only one file, in case // there was multiple files selected var file = e.currentTarget.files[0]; if (file) { let uploadInstance = FilesCol.insert({ file: file, meta: { locator: self.props.fileLocator, userId: this.props.uidProvisional || Meteor.userId() // Optional, used to check on server for file tampering }, chunkSize: 'dynamic', allowWebWorkers: true // If you see issues with uploads, change this to false }, false) self.setState({ uploading: uploadInstance, // Keep track of this instance to use below inProgress: true // Show the progress bar now }); // These are the event functions, don't need most of them, it shows where we are in the process uploadInstance.on('start', function () { console.log('Starting'); }) uploadInstance.on('end', function (error, fileObj) { console.log('On end File Object: ', fileObj); //this.props?.setAvatarId(fileObj._id); }) uploadInstance.on('uploaded', function (error, fileObj) { console.log('uploaded: ', fileObj); //console.log(`setAvatarId: ${this.props?.setAvatarId}`); // this.props.setUpId(fileObj._id); // Remove the filename from the upload box self.refs['fileinput'].value = ''; // Reset our state for the next file self.setState({ uploading: [], progress: 0, inProgress: false }); }) uploadInstance.on('error', function (error, fileObj) { console.log('Error during upload: ' + error) }); uploadInstance.on('progress', function (progress, fileObj) { console.log('Upload Percentage: ' + progress) // Update our progress bar self.setState({ progress: progress }); }); uploadInstance.start(); // Must manually start the upload } } } // This is our progress bar, bootstrap styled // Remove this function if not needed showUploads() { console.log('**********************************', this.state.uploading); if (!_.isEmpty(this.state.uploading)) { return
Upload New File: