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 :: multiple click events in react 
Javascript :: nextjs multer rename file 
Javascript :: how to launch several async functions in node js 
Javascript :: refresh a page in the browser node js 
Javascript :: react native modal not full screen 
Javascript :: javaSript string first words to upper case 
Javascript :: stomp.min.js cdn 
Javascript :: Change the HTML of an element 
Javascript :: js sort by date 
Javascript :: dynamic import javascript 
Javascript :: npm shrinkwrap primordials 
Javascript :: javascript remove style 
Javascript :: user focus on tab javascript 
Javascript :: Min Stack Algorithm JS 
Javascript :: JSON.parse() error handling 
Javascript :: get data from formdata 
Javascript :: cannot get issue in nodejs 
Javascript :: electron no resize 
Javascript :: chalk nodeks 
Javascript :: select dropdown value using jquery 
Javascript :: append child at the top 
Javascript :: React JS CDN Links 
Javascript :: regex to check if text is in quotations 
Javascript :: short if 
Javascript :: javascript change css 
Javascript :: addclass removeclass jquery 
Javascript :: cdn jquery 
Javascript :: how to drop collection in mongoose 
Javascript :: fs readfile not working 
Javascript :: cypress check attribute for each element 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =