Quasar Framework Uploader with axios

倾然丶 夕夏残阳落幕 提交于 2020-04-06 22:20:11

问题


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


回答1:


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

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.

V1 Update

Use @added -> function(files) method.



来源:https://stackoverflow.com/questions/53784888/quasar-framework-uploader-with-axios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!