Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Get filereader result in nextjs Image

I'm trying to implement image preview before file upload in NextJs. When I read the result and try to display it doesn't work as usual. The image breaks. I have added the code below. Any inputs appreciated to sort this problem.

Edit: Partially solved this by converting the buffer to blob url. It works perfectly with img tag but next/image still doesn't support blob urls.

code

import React, { useCallback } from 'react';
import { useDropzone } from 'react-dropzone';
import Image from 'next/image';
import australia from '../public/images/australia.png';

const Create: React.FC<{ onClose: () => void }> = ({ onClose }) => {
    
    const [uploadedImage, setUploadedImage] = React.useState<any>();
    
    const onDrop = useCallback(acceptedFiles => {
        acceptedFiles.forEach((file) => {
            const reader = new FileReader()
    
            reader.onabort = () => console.log('file reading was aborted')
            reader.onerror = () => console.log('file reading has failed')
            reader.onload = () => {
                // Do whatever you want with the file contents
                const binaryStr = reader.result
                //changed this to blob url working perfectly with img tag not with Image tag.
                setUploadedImage(URL.createObjectURL(file));
                }
                reader.readAsArrayBuffer(file)
            })
        }, []);
    return (
            <>
               <div {...getRootProps()} className="p-7 border-2 border-dashed my-5 focus:border-indigo-500">
                <input {...getInputProps()} />
                {
                    isDragActive ?
                        <p>Drop the files here...</p> : <p>Drag 'n' drop some files here, or click to select files</p>
                }
                </div>
                <img src={uploadedImage || '/images/australia.png'} />
            </>
    );

Error Console error description after image upload

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda