Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

axios multipart/form-data

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

send multipart form data axios with node js

const axios = require('axios');
const FormData = require('form-data');

const form = new FormData();
// Second argument  can take Buffer or Stream (lazily read during the request) too.
// Third argument is filename if you want to simulate a file upload. Otherwise omit.
form.append('field', 'a,b,c', 'blah.csv');
axios.post('http://example.org/endpoint', form, {
  headers: form.getHeaders(),
}).then(result => {
  // Handle result…
  console.log(result.data);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript last character of a string 
Javascript :: date format in jquery 
Javascript :: what does sanitize do javascript 
Javascript :: numero random en js 
Javascript :: json to string 
Javascript :: Using vuejs-datepicker with Nuxt JS 
Javascript :: javascript download image 
Javascript :: angular countdown begin stop pause 
Javascript :: defer parsing of javascript avada 
Javascript :: singleton function javascript 
Javascript :: why did you render 
Javascript :: reference of event listener funtion to remove 
Javascript :: momentjs get calendar week 
Javascript :: remove storybook from project 
Javascript :: js change classlist 
Javascript :: js session storage 
Javascript :: json comment 
Javascript :: import an image react in the public folder 
Javascript :: d3 paning 
Javascript :: how can hide link from inspect element 
Javascript :: check empty object javascript 
Javascript :: use of map in react 
Javascript :: json to pdf javascript 
Javascript :: JavaScript block-scoped Variable9 
Javascript :: get total pairs from integer array javascript 
Javascript :: inline javascript modules 
Javascript :: stdout javascript 
Javascript :: center canvas p5js 
Javascript :: jquery add attribute without value 
Javascript :: mongoose updatemany example 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =