ryanto / next-s3-upload

Upload files from your Next.js app to S3
https://next-s3-upload.codingvalue.com/
543 stars 76 forks source link

Just not working #48

Closed oelbaga closed 2 years ago

oelbaga commented 2 years ago

This seemed so promising, but a simple copy and paste doesn't work. using the basic file example. When you click upload a file and upload an image there's just no response, nothing in the console, no state change, nothing happens the page stays as is.

Simply using the demo code:

import { useState } from 'react';
import { useS3Upload } from 'next-s3-upload';

export default function UploadTest() {
  let [imageUrl, setImageUrl] = useState();
  let { FileInput, openFileDialog, uploadToS3 } = useS3Upload();

  let handleFileChange = async file => {
    let { url } = await uploadToS3(file);
    setImageUrl(url);
  };

  return (
    <div>
      <FileInput onChange={handleFileChange} />

      <button onClick={openFileDialog}>Upload file</button>

      {imageUrl && <img src={imageUrl} />}
    </div>
  );
}
montanaflynn commented 1 year ago

Same issue but it's intermittent and I can't always reproduce it. The issue appears to be that handleFileChange just doesn't fire after selecting a file sometimes.

import { useState } from 'react';
import { useS3Upload } from 'next-s3-upload';

export default function UploadTest() {
  let [imageUrl, setImageUrl] = useState();
  let { FileInput, openFileDialog, uploadToS3 } = useS3Upload();

  let handleFileChange = async file => {
    alert("upload")
    let { url } = await uploadToS3(file);
    setImageUrl(url);
  };

  return (
    <div>
      <FileInput onChange={handleFileChange} />

      <button onClick={openFileDialog}>Upload file</button>

      {imageUrl && <img src={imageUrl} />}
    </div>
  );
}

Added an alert, about 50% of the time after choosing an image it happens, the other nothing happens.

ryanto commented 1 year ago

@montanaflynn Thank you for doing some testing!

I'd love to be able to follow your exact steps to help debug this.

Few questions:

montanaflynn commented 1 year ago

@ryanto

https://codesandbox.io/s/sharp-shamir-d787sl?file=/src/App.js

ryanto commented 1 year ago

Ok cool! Thank you for setting this all up. I'll take a look and let you know what I find.

JackWCollins commented 1 year ago

Just wanted to chime in that I'm also experiencing this same issue. I am using NextJs v13, so that could be part of the problem. Can/should this be reopened?

I was able to get around this by not using the FileInput and instead just using:

  const startUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
    if (!event.target.files) {
      console.error('No file selected')
      return
    }

    const file = event.target.files[0]
    let { url } = await uploadToS3(file);
    setImageUrl(url);
  }

  return (
    <div>
      <input type="file" onChange={startUpload} />

      {imageUrl && <img src={imageUrl} />}
    </div>
  );
montanaflynn commented 1 year ago

I was using next 12, and ended up building the functionality from scratch.

ryanto commented 1 year ago

I'd say don't use FileInput and building from scratch is the way to go if it's not working for you.