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 :: sub function javascript 
Javascript :: react native generate signed apk getting older version 
Javascript :: console log all array values node 
Javascript :: javascript get all elements by class starting with 
Javascript :: spread operator react array 
Javascript :: spotify player react 
Javascript :: running webpack application on production server 
Javascript :: javascript print square 
Javascript :: discord.js lock channel 
Javascript :: nodejs read file to array 
Javascript :: javascript wait for async function to finish 
Javascript :: default function parameters javascript 
Javascript :: cheerio each 
Javascript :: how to clear nodejs terminal in vs code 
Javascript :: discord.js add role command 
Javascript :: close browser tab using jquery 
Javascript :: currying function callback javascript 
Javascript :: javascript function with input value 
Javascript :: browser support fetch api 
Javascript :: javascript interface class 
Javascript :: remove last word from string javascript 
Javascript :: creating a custom function to use nodemailer to send email 
Javascript :: javascript closest child 
Javascript :: jquery view image in codeigniter 
Javascript :: windows 10 retiré le theme sombre explorateur 
Python :: pygame disable message 
Python :: seaborn figsize 
Python :: python count files directory 
Python :: pandas convert string from INT TO str 
Python :: python sleep random 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =