Vue implementation file upload

  Should file upload web, it requires several steps?

  A: The three-step

  Step 1: Create a label file uploads 

  <input type="file" id="fileExport" @change="handleFileChange" ref="inputer">
  Because it is in vue, considering acquiring node, add a ref to tag, to facilitate access to the properties and add events to the label
  
  Step two: change event
  
  handleFileChange (e) {
    let inputDOM = this.$refs.inputer;
    this.file = inputDOM.files [0]; // data file by taking DOM
    let size = Math.floor (this.file.size / 1024); // calculate the size of the file 
    = new new FormData this.formData (); // a new new formData event
    this.formData.append ( "file", this.file); // add a file attribute to formData in
    // At this point we would like to formData is a backstage pass parameters
  }
  
  Step Three: Upload formData    
  this.$http({
    url:this.HOST + api.upload,
    data: formData, // upload files here
    method: "post",
    headers:{
      'Content-Type': 'multipart / form-data' // It is worth noting that this must take place about the change request header
    }
    }).then(function(res){
       console.log (res, "here should request a callback successful")  
    }.catch(function(req){
        console.log (req, "callback request fails, see for yourself why failure")
    })
})
  
 
  

Guess you like

Origin www.cnblogs.com/-moon/p/11375123.html