F
F
fomenko_alexandr2017-10-01 15:09:56
PHP
fomenko_alexandr, 2017-10-01 15:09:56

How to send a file in vue.js so that it gets into $_FILES?

Hello.
How can I send a file so that the data does not go to php://input, but is available directly from $_FILES?
Or by the same $_POST Request, when I send the data, they are not available in the $_POST array, but again, in php://input
Please tell me how can I get around this?

Now the code for sending files is as follows:
<script>
    new Vue({
        el: '#app',
        data: {
            files: []
        },
        methods: {
            onFileChange: function(e) {
                var files = e.target.files || e.dataTransfer.files;
                if (!files.length)
                    return;
                this.createImage(files[0]);
            },
            createImage: function(file) {
                var reader = new FileReader();
                var vm = this;
                reader.onload = function(e) {
                    vm.image = e.target.result;
                };
                reader.readAsDataURL(file);
            },
            upload: function(){
                axios.post('/file', {image: this.image}})
                    .then(function(res) {
                    console.log(res);
                    });
            }
        }
    })
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem0071, 2017-10-01
@fomenko_alexandr

use FormData
Something like this will work:

let data = new FormData();
data.append('file', thi.image);

axios.post('/file', data)
                    .then(function(res) {
                    console.log(res);
                    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question