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 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 :: chrome storage set example 
Javascript :: backtick string javascript 
Javascript :: jquery document ready shorthand 
Javascript :: equality operator javascript 
Javascript :: chart js two layer label 
Javascript :: reverse integer in javascript 
Javascript :: mouse wheel event angular for table 
Javascript :: how to include js file in react 
Javascript :: javascript if not 
Javascript :: ajax form submit without form tag 
Javascript :: jquery json to table 
Javascript :: javascript array contains 
Javascript :: javascript reverse each string element in array 
Javascript :: Duplicate empty header occur in datatable when enabling scrollX or scrollY when using Google Chrome 
Javascript :: string into json javascript 
Javascript :: use theme in component mui 
Javascript :: 1 dollar in rupees 
Javascript :: nodejs return value 
Javascript :: jquery recharger la page 
Javascript :: js new element 
Javascript :: reverse js 
Javascript :: js operators 
Javascript :: call multiple functions onclick react 
Javascript :: build#configuring-commonjs-dependencie - angular.json 
Javascript :: array.slice 
Javascript :: get milliseconds since epoch for 12am today javascript 
Javascript :: node check if internet 
Javascript :: javascript hello world 
Javascript :: how to change list item text color in react 
Javascript :: next connect 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =