Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit is doing when select a file #4

Closed
luisfuertes opened this issue Jan 3, 2017 · 6 comments
Closed

Submit is doing when select a file #4

luisfuertes opened this issue Jan 3, 2017 · 6 comments

Comments

@luisfuertes
Copy link

When i select a file, before i press submit button. File is uploaded and handleUploadSuccess is called.

How can i send file when i click on submit button, instead of when i pick a file?

Thanks!

@sprmn
Copy link
Collaborator

sprmn commented Jan 3, 2017

Hi Luis, thanks for opening an issue. I'd love to further improve this library.

If I understand you correctly, you don't like it that the uploader starts uploading right after selecting a file, probably because it is a waste of storage if a user selects a different file before submitting the form.

I could provide a property that disables the auto upload and provide a function that allows you to start the upload manually. However, I don't know if it would be very valuable. Most of the time you will need to be sure the file is successfully uploaded before you submit the form.

Anyways, if you disagree, could you provide me a bit more info about your use case?

I am currently working on an update that will allow you to easily render a custom styled button. Are there any other problems / things you'd like to see in the component?

@luisfuertes
Copy link
Author

Thanks for answer!

 you don't like it that the uploader starts uploading right after selecting a file, probably because it is a waste of storage if a user selects a different file before submitting the form.

Yes, i want that, upload only on press submit button. Not after select file. And if you could implement multiple upload (two images at same time) that would be fantastic

@ufon
Copy link

ufon commented Jan 15, 2017

+1

@sprmn
Copy link
Collaborator

sprmn commented Feb 3, 2017

Sorry for my late reply, been very busy.

Preventing the auto upload is already possible in the current situation. You can provide your own onChange function to the uploader, to handle file change and call the startUpload function of the uploader upon form submission, with the file as a first argument, to start the upload procedure.

Small example (not tested):

import React, { Component } from 'react';
import { render } from 'react-dom';
import firebase from 'firebase';
import config from './firebase-config.json'; // provide your firebase credentials in this file
import FileUploader from 'react-firebase-file-uploader';

firebase.initializeApp(config);

class Example extends Component {
  handleChangeImage = (e) => {
    const image = e.target.files[0];
    if (image) {
      this.setState({image});
    }
  };
  handleUploadSuccess = (filename) => {
    // Do something with the name of the uploaded file and possibly the rest of the form
    firebase.database().ref('images').push({filename});
  };
  handleSubmit = (e) => {
    e.preventDefault();
    if (this.uploader && this.state.image) {
      this.uploader.startUpload(this.state.image);
    }
}

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <label>Image:</label>
          <FileUploader
            ref={c => { this.uploader = c; }}
            accept="image/*"
            randomizeFilename
            storageRef={firebase.storage().ref('images')}
            onChange={this.handleChangeImage}
            onUploadSuccess={this.handleUploadSuccess}
          />
          <button type="submit" />
        </form>
      </div>
    );
  }
}

render(
  <Example />,
  document.getElementById('root')
);

I will try to add a mutli upload functionality within the next 2 weeks.

@sprmn sprmn closed this as completed May 9, 2017
@jamdog13
Copy link

Just wanted to drop by and thank you for that example, it was very useful.

@jadrum
Copy link

jadrum commented Jun 8, 2018

in your example above I was slightly confused by the phrase "this.uploader"... I can't figure out what it is referring to.. any help @sprmn ? thanks!

nvm, figured out what you were doing in the ref portion... overlooking easy things sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants