Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

upload files in react using axios

import React from 'react';
import axios from 'axios';

const Form = () => {
  // a local state to store the currently selected file.
  const [selectedFile, setSelectedFile] = React.useState(null);

  const handleSubmit = (event) => {
    event.preventDefault()
    const formData = new FormData();
    formData.append("selectedFile", selectedFile);
    try {
      const response = await axios({
        method: "post",
        url: "/api/upload/file",
        data: formData,
        headers: { "Content-Type": "multipart/form-data" },
      });
    } catch(error) {
      console.log(error)
    }
  }

  const handleFileSelect = (event) => {
    setSelectedFile(event.target.files[0])
  }

  return (
    <form onSubmit={handleSubmit}>
      <input type="file" onChange={handleFileSelect}/>
      <input type="submit" value="Upload File" />
    </form>
  )
};

export default Form;
Comment

PREVIOUS NEXT
Code Example
Javascript :: check type string javascript 
Javascript :: jquery on click get element 
Javascript :: sequelize generate migration 
Javascript :: window.location.href target blank 
Javascript :: jquery grid get all selected row 
Javascript :: remove non-alphanumeric characters and space javascript 
Javascript :: generate random base 64 string js 
Javascript :: kendo dropdownlist value jquery 
Javascript :: foreach nodejs 
Javascript :: regx get only domain name from url 
Javascript :: react hide source code 
Javascript :: get input value jquery 
Javascript :: ajax clear form 
Javascript :: check if enter key is pressed jquery 
Javascript :: is array equal javascript 
Javascript :: javascript button go to url 
Javascript :: how to get date time day year in javscript 
Javascript :: discord.js change bot status 
Javascript :: javascript get domain 
Javascript :: how to append only once in jquery 
Javascript :: decet wheter react app in development or production 
Javascript :: Check if a JavaScript string is a URL 
Javascript :: how to hover the mouse on an element cypress mouseover 
Javascript :: how to copy text in react 
Javascript :: window.history.pushstate typescript 
Javascript :: check given path is valid or not in nodejs 
Javascript :: javascript Count the occurrences of a value in an array 
Javascript :: protractor move mouse and click 
Javascript :: javascript remoev css class 
Javascript :: javascript assign value to input using name 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =