POST file along with form data Vue + axios

后端 未结 2 460
深忆病人
深忆病人 2020-12-31 17:58

I have a method for Vuejs component:

async submit () {
        if (this.$refs.form.validate()) {
          let formData = new FormData()
          formData.a         


        
2条回答
  •  忘掉有多难
    2020-12-31 18:45

    PHP ( process.php )

     $_POST,
            "files" => $_FILES
        );
    
        echo json_encode($data);
    ?>
    

    Vue and form HTML

    let vm = new Vue({
        el: "#myApp",
        data: {
            form: {}
        },
        methods: {
            submit: async function (e) {
                e.preventDefault();
    
                /* formData */
                var formData = new FormData( this.$refs.formHTML );
    
                /* AJAX request */
                await axios({
                    method: "post",
                    url: "process.php",
    
                    data: formData,
    
                    config: { headers: { "Content-Type": "multipart/form-data" } }
                })
    
                /* handle success */
                .then( response => { console.log(response.data); } )
    
                /* handle error */
                .catch( response => { console.log(response) } );
            }
        }
    });
    
    
    
    
    Name:
    Gender: Male Female
    File:

提交回复
热议问题