how to submit “multipart/form-data” from VueJs

前端 未结 3 537
太阳男子
太阳男子 2021-01-14 04:58

I am using VueJs / axios in frontend and multer in nodejs for a simple file upload to work.

So far all tries has been unsuccessful. While this can be achieved in 1

3条回答
  •  失恋的感觉
    2021-01-14 05:36

    You can use FormData for that and it is pretty easy.

    Let me show you an example:

    // html    
    
    
    // js
    methods: {
        upload () {
            let files = this.$refs.uploadBtn.files
            let formData = new FormData()
    
            // if you want to upload multiple files at once loop 
            // through the array of files
            formData.append('attachment', files[0])
            axios.post(url, formData).then(response => ...)
        }
    }
    

    This should do the trick and you don't really need a 3rd party plugin for that.

提交回复
热议问题