Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

next js react image upload

import { useState } from "react";

/**
 * # MY ACCOUNT GOOGLE PLAY:
 * @see {@link https://play.google.com/store/apps/developer?id=dzino Google Play}
 */

export default function PrivatePage(props) {
  const [image, setImage] = useState(null);
  const [createObjectURL, setCreateObjectURL] = useState(null);

  const uploadToClient = (event) => {
    if (event.target.files && event.target.files[0]) {
      const i = event.target.files[0];

      setImage(i);
      setCreateObjectURL(URL.createObjectURL(i));
    }
  };

  const uploadToServer = async (event) => {
    const body = new FormData();
    body.append("file", image);
    const response = await fetch("/api/file", {
      method: "POST",
      body
    });
  };

  return (
    <div>
      <div>
        <img src={createObjectURL} />
        <h4>Select Image</h4>
        <input type="file" name="myImage" onChange={uploadToClient} />
        <button
          className="btn btn-primary"
          type="submit"
          onClick={uploadToServer}
        >
          Send to server
        </button>
      </div>
    </div>
  );
}

Comment

upload image to server next js

const [image, setImage] = useState(null);
  const [createObjectURL, setCreateObjectURL] = useState(null);

  const uploadToClient = (event) => {
    if (event.target.files && event.target.files[0]) {
      const i = event.target.files[0];
      setImage(i);
      setCreateObjectURL(URL.createObjectURL(i));
    }
  };
  
  const uploadToServer = async (event) => {
    const body = new FormData();
    body.append("file", image);
    const response = await fetch("/api/file", {
      method: "POST",
      body
    });
  };


 <img src={createObjectURL} />
        <h4>Select Image</h4>
        <input type="file" name="myImage" onChange={uploadToClient} /> */}
         <button
          className="btn btn-primary"
          type="submit"
          onClick={uploadToServer}
        >
          Send to server
        </button> 
Comment

upload image to server react next

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component<{}, {}> {
// Uploader component
  public path: object = {
    removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove',
    saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save'
  }
public render(): JSX.Element{
    return (
        <UploaderComponent asyncSettings = {this.path} minFileSize = {10000} maxFileSize= {28400000}
        />
        )
    }
}
ReactDOM.render(<App />, document.getElementById('fileupload'));
Comment

show uploaded image in react/nextjs

useEffect(() => {
   // create the preview
   const objectUrl = URL.createObjectURL(selectedFile)
   setPreview(objectUrl)

   // free memory when ever this component is unmounted
   return () => URL.revokeObjectURL(objectUrl)
}, [selectedFile])
Comment

PREVIOUS NEXT
Code Example
Javascript :: disabling first item in dropdownlist 
Javascript :: joi for validation 
Javascript :: typeorm sqlite Using Repositories 
Javascript :: tekenaja 
Javascript :: crear un texto dinamicamente con javascript 
Javascript :: Starting the project 
Javascript :: javascript camel case to words 
Javascript :: req.parms en react js 
Javascript :: alertify.js styled success messae 
Javascript :: javascript float not showing 0 
Javascript :: verify number of times request was made in cypress 
Javascript :: send data with next 
Javascript :: Pass Props to a Component Using Short circuit evaluation in react 
Javascript :: how to change sender name in nodemailer 
Javascript :: prevent the fire of parent event listener 
Javascript :: Async restricted or not 
Javascript :: multiple counter for loop in javascript 
Javascript :: close worker 
Javascript :: how to print huge numbers in a variable alert javascript 
Javascript :: react native delay input 
Javascript :: json mapper 
Javascript :: render(<App /); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); 
Javascript :: react call component state 
Javascript :: google sheets array formula ignore blank cells 
Javascript :: NodeJS: Good way to write Multiple API Calls in serial 
Javascript :: https://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model 
Javascript :: reading data from link in javascript 
Javascript :: como fazer piramade de asteriscos 
Javascript :: how to append response header in node in every request 
Javascript :: everything be true freecodecamp 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =