$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - React-js: How to upload Image with preview and display the processe image - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React-js: How to upload Image with preview and display the processe image - Stack Overflow

matteradmin13PV0评论

Looking for some help in creating a React-js ponent to upload an image with preview and display the returned processed Image. API server return a binary png and not a url to the image. Resulting image should be displayed in a separate Div

Regards

I am not able to find a good example to upload image to a API server and display the returned binary image file

Looking for some help in creating a React-js ponent to upload an image with preview and display the returned processed Image. API server return a binary png and not a url to the image. Resulting image should be displayed in a separate Div

Regards

I am not able to find a good example to upload image to a API server and display the returned binary image file

Share Improve this question edited Nov 22, 2022 at 17:16 Vuely AI asked Nov 22, 2022 at 17:09 Vuely AIVuely AI 552 silver badges5 bronze badges 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Nov 22, 2022 at 18:50
Add a ment  | 

1 Answer 1

Reset to default 5

Use this code as a sample:

const MyComponent = () => {
    const [previewImage, setPreviewImage] = useState(null);
    const [uploadedImage, setUploadedImage] = useState(null);

    const handleUploadImage = () => {
        const data = new FormData();
        data.append('files[]', previewImage);

        fetch(/* server url */, { method: 'POST', body: data }).then(async (response) => {
            const imageResponse = await response.json();
            setUploadedImage(imageResponse);
        }).catch((err) => {

        });
    }

    const handleSelectImage = (event) => {
        const file = event.target.files[0];
        const fileReader = new FileReader();
        fileReader.addEventListener("load", () => {
            setPreviewImage(fileReader.result);
        });
        fileReader.readAsDataURL(file);
    }

    return (
        <div>
            <input type="file" onChange={handleSelectImage} />
            {
                previewImage ?
                    <img src={previewImage} alt="preview-image" />
                :
                    null
            }
            {
                uploadedImage ?
                    <img src={uploadedImage} alt="uploaded-image" />
                :
                    null
            }
            <button onClick={handleUploadImage}>Upload</button>
        </div>
    );
}
Post a comment

comment list (0)

  1. No comments so far