Search code examples
vue.jsaxiosuploaderquasar-framework

Quasar Framework Uploader with axios


A question about the quasar framework uploader component. I need to post the images to a URL that will rename the uploaded file and return the full path.

I'm using the upload-factory and axios

But I'm having problems understanding exactly how to pass the file to axios as if it was just an input type file. Basically I need to make it as If I'm sending a form with a single input file like this:

<input type="file" name="banner">

This is the component:

<q-uploader
  url=""
  extensions=".gif,.jpg,.jpeg,.png"
  :filter="filterFiles"
  :upload-factory="uploadFile" />

This is the upload method, but I keep getting an error response from the server.

uploadFile (file, updateProgress) {
  const formData = new FormData()
  formData .set('banner', file)
  var headers = {
    'Content-Type': 'multipart/form-data'
  }
  axios.post('http://someurl/uploadFile', formData , headers)
    .then(function (response) {
      console.log(response)
    })
    .catch(function (response) {
      console.log(response)
    })
}

If I post a plain html form with method="post" enctype="multipart/form-data" and a

<input type="file" name="banner">

I get my OK response from the server with the processed/uploaded image URL


Solution

  • I have successfully done uploading of the file to python API.


    V1 Update

    Use @added -> function(files) method.

    Also extensions property is removed now use accept


    This is the component:

    <q-uploader
      url=""
      extensions=".gif,.jpg,.jpeg,.png"
      @add="file_selected"
    />
    
    <q-btn @click="uploadFile()">Upload</q-btn>
    

    Data:

    data() {
      return {
        selected_file:'',
        check_if_document_upload:false,
      }
    }
    

    Methods

    file_selected(file) {
      this.selected_file = file[0];
      this.check_if_document_upload=true;
    },
    
    uploadFile() {
      let fd = new FormData();
      fd.append("file", this.selected_file);
    
      axios.post('/uploadFile', fd, {
        headers: { 'Content-Type': undefined },
      }).then(function(response) {
        if(response.data.ok) {
        
        }
      }.bind(this));
    }
    

    This is working fine for me.