Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

upload multiple files axios

// silly note but make sure you're constructing files for these (if you're recording audio or video yourself)
// if you send it something other than file it will fail silently with this set-up 
let arrayOfYourFiles=[image, audio, video]
// create formData object
const formData = new FormData();
arrayOfYourFiles.forEach(file=>{
  formData.append("arrayOfFilesName", file);
});

axios({
  method: "POST",
  url: serverUrl + "/multiplefiles",
  data: formData,
  headers: {
    "Content-Type": "multipart/form-data"
  }
})
//some error handling
Comment

upload and send file to axios multipart

import React from 'react'
import axios, { post } from 'axios';

class SimpleReactFileUpload extends React.Component {

  constructor(props) {
    super(props);
    this.state ={
      file:null
    }
    this.onFormSubmit = this.onFormSubmit.bind(this)
    this.onChange = this.onChange.bind(this)
    this.fileUpload = this.fileUpload.bind(this)
  }

  onFormSubmit(e){
    e.preventDefault() // Stop form submit
    this.fileUpload(this.state.file).then((response)=>{
      console.log(response.data);
    })
  }

  onChange(e) {
    this.setState({file:e.target.files[0]})
  }

  fileUpload(file){
    const url = 'http://example.com/file-upload';
    const formData = new FormData();
    formData.append('file',file)
    const config = {
        headers: {
            'content-type': 'multipart/form-data'
        }
    }
    return  post(url, formData,config)
  }

  render() {
    return (
      <form onSubmit={this.onFormSubmit}>
        <h1>File Upload</h1>
        <input type="file" onChange={this.onChange} />
        <button type="submit">Upload</button>
      </form>
   )
  }
}



export default SimpleReactFileUpload
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to copyy a string variable to clipboard in js 
Javascript :: javascript map to object 
Javascript :: react algolia range slider 
Javascript :: ajax get request parameters 
Javascript :: sub array javascript 
Javascript :: joi regex validate 
Javascript :: pass param to url retrofit 
Javascript :: nuxt auth keep user loggedin on refresh 
Javascript :: added font to react native 
Javascript :: toastify react not working 
Javascript :: jquery date format 
Javascript :: multiple conditions for JavaScript .includes() method 
Javascript :: cancel an event in javascript 
Javascript :: Beautifule JS Console Log 
Javascript :: using hooks with apis 
Javascript :: appendchild on specific position 
Javascript :: how to check if the number inputed is number 
Javascript :: foreach loop google script 
Javascript :: javascript get params from query string json object 
Javascript :: Using the reverse method to Reverse an Array 
Javascript :: reddit fetch api js 
Javascript :: vuejs reset component 
Javascript :: update data in json using javascript 
Javascript :: datatable highlight cells based on their content 
Javascript :: webpack url loader not working 
Javascript :: how to resize image in react js 
Javascript :: framer motion for react 
Javascript :: mongoose nested object without id 
Javascript :: get smallest value in array js 
Javascript :: add a child html object to another html object in js 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =