2

I learn React and now I use the Uppy so user can select files for upload.

When user have select his file the files are hidden by settting showSelectedFiles={false}

I use my own Component to show the selected files and I get the files using this:

  .on("file-added", (file) => {
    const { setFile } = props;
    setFile(file);
    const newList = this.state.files.concat({ file });
    this.setState({
      files: { newList },
    });
  });

For each file added to the Dashboard the setFile(file); is sending the file object to my Custom view. The problem is that the preview image Blob that is auto created by the Dashboard is not present at this stage.

enter image description here

How can I get the files to my Custom GUI to show them including the image preview Blob?

I'm new to React and JavaScript so please be gentle:)

Complete code:

import React from "react";

import "@uppy/status-bar/dist/style.css";
import "@uppy/drag-drop/dist/style.css";
import "@uppy/progress-bar/dist/style.css";
import "./styles.css";
import "@uppy/core/dist/style.css";
import "@uppy/dashboard/dist/style.css";

const Uppy = require("@uppy/core");
// const Dashboard = require("@uppy/dashboard");
const GoogleDrive = require("@uppy/google-drive");
const Dropbox = require("@uppy/dropbox");
const Instagram = require("@uppy/instagram");
const Webcam = require("@uppy/webcam");
const Tus = require("@uppy/tus");
const ThumbnailGenerator = require("@uppy/thumbnail-generator");

const {
  Dashboard,
  DashboardModal,
  DragDrop,
  ProgressBar,
} = require("@uppy/react");

class DashboardUppy extends React.Component {
  constructor(props) {
    super(props);
    this.form = React.createRef();
    this.state = {
      showInlineDashboard: false,
      open: false,
      files: [],
    };

    this.uppy = new Uppy({
      id: "uppy1",
      autoProceed: false,
      debug: true,
      allowMultipleUploads: true,
      proudlyDisplayPoweredByUppy: true,
      restrictions: {
        // maxFileSize: 1000000,
        maxNumberOfFiles: 100,
        minNumberOfFiles: 1,
        allowedFileTypes: null,
      },
      onBeforeFileAdded: (currentFile, files) => {
        console.log(files);
        const modifiedFile = Object.assign({}, currentFile, {
          name: currentFile + Date.now(),
        });
        if (!currentFile.type) {
          // log to console
          this.uppy.log(`Skipping file because it has no type`);
          // show error message to the user
          this.uppy.info(`Skipping file because it has no type`, "error", 500);
          return false;
        }
        return modifiedFile;
      },
    })
      .use(Tus, { endpoint: "https://master.tus.io/files/" })
      .use(GoogleDrive, { companionUrl: "https://companion.uppy.io" })
      .use(Dropbox, {
        companionUrl: "https://companion.uppy.io",
      })
      .use(Instagram, {
        companionUrl: "https://companion.uppy.io",
      })
      .use(Webcam, {
        onBeforeSnapshot: () => Promise.resolve(),
        countdown: false,
        modes: ["video-audio", "video-only", "audio-only", "picture"],
        mirror: true,
        facingMode: "user",
        locale: {
          strings: {
            // Shown before a picture is taken when the `countdown` option is set.
            smile: "Smile!",
            // Used as the label for the button that takes a picture.
            // This is not visibly rendered but is picked up by screen readers.
            takePicture: "Take a picture",
            // Used as the label for the button that starts a video recording.
            // This is not visibly rendered but is picked up by screen readers.
            startRecording: "Begin video recording",
            // Used as the label for the button that stops a video recording.
            // This is not visibly rendered but is picked up by screen readers.
            stopRecording: "Stop video recording",
            // Title on the “allow access” screen
            allowAccessTitle: "Please allow access to your camera",
            // Description on the “allow access” screen
            allowAccessDescription:
              "In order to take pictures or record video with your camera, please allow camera access for this site.",
          },
        },
      }).use(ThumbnailGenerator, {
        thumbnailWidth: 200,
        // thumbnailHeight: 200 // optional, use either width or height,
        waitForThumbnailsBeforeUpload: true
      })
      .on("thumbnail:generated", (file, preview) => {
        const img = document.createElement("img");
        img.src = preview;
        img.width = 100;
        document.body.appendChild(img);
      })
      .on("file-added", (file) => {
        const { setFile } = props;
        setFile(file);
        const newList = this.state.files.concat({ file });
        this.setState({
          files: { newList },
        });
      });
  }

  componentWillUnmount() {
    this.uppy.close();
  }

  render() {
    const { files } = this.state;
    this.uppy.on("complete", (result) => {
      console.log(
        "Upload complete! We’ve uploaded these files:",
        result.successful
      );
    });

    return (
      <div>
        <div>
            <Dashboard
              uppy={this.uppy}
              plugins={["GoogleDrive", "Webcam", "Dropbox", "Instagram"]}
              metaFields={[
                { id: "name", name: "Name", placeholder: "File name" },
              ]}
              open={this.state.open}
              target={document.body}
              onRequestClose={() => this.setState({ open: false })}
              showSelectedFiles={false}
            />
        </div>
      </div>
    );
  }
}

export default DashboardUppy;
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Kid
  • 1,869
  • 3
  • 19
  • 48

1 Answers1

0

Ran into this problem as well because I wanted to use the image preview to figure out the aspect ratio of the underlying image.

If you're using Dashboard or ThumbnailGenerator for Uppy, an event is emitted for every upload:

uppy.on('thumbnail:generated', (file, preview) => {
  const img = new Image();
  img.src = preview;
  img.onload = () => {
    const aspect_ratio = img.width / img.height;
    // Remove image if the aspect ratio is too weird.
    // TODO: notify user.
    if (aspect_ratio > 1.8) {
      uppy.removeFile(file.id);
    }
  }
});

I realize though that you already are looking for this event in your code. I guess to answer your question, just put your logic there instead of in file-added.

getup8
  • 6,949
  • 1
  • 27
  • 31