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 :: on load of jquery 
Javascript :: json stringify indent 
Javascript :: hide element js 
Javascript :: prettier semicolon false in javascript 
Javascript :: match date regex 
Javascript :: open google chrome in puppeteer macos 
Javascript :: add 10 seconds to date javascript 
Javascript :: deploy react app netlify 
Javascript :: nodejs get response time 
Javascript :: jquery get relative position of element 
Javascript :: javascript log Time from Date 
Javascript :: change swiper-slide width angular 
Javascript :: filter special characters javascript 
Javascript :: big numbers factorial js 
Javascript :: hide div js 
Javascript :: js scroll page horizontally with mouse wheel 
Javascript :: calculate string value in javascript, not using eval 
Javascript :: odd even condition with ternary operator in javaScript 
Javascript :: current year javascript 
Javascript :: how to delete the last part of a string in node js 
Javascript :: javascript order by string array 
Javascript :: How can I refresh a page with jQuery 
Javascript :: slug generator javascript 
Javascript :: redirect to url in javascript 
Javascript :: react form on submit 
Javascript :: Javascript looping through table 
Javascript :: console redux state shows proxy 
Javascript :: jquery get selected text from multiselect 
Javascript :: react media query hook 
Javascript :: average of an array js 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =