R
R
Ruslan Absalyamov2018-11-30 17:10:29
Vue.js
Ruslan Absalyamov, 2018-11-30 17:10:29

How to upload a file from a form?

How to upload a file through forms

<template>
<div class="form-avatar">
            <label>
                <img src="#" alt="Аватар">
                <input type="file" v-show="false" @change="fileInput($event)">
            </label>
        </div>
</template>

<script>
    export default {
        name: "create",
        methods: {
          fileInput(event) {
              console.log(event.target.files[0])
          }
        }
    }
</script>

that is, I can get the file, but how can I save it now in my local area?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2018-11-30
@rusline18

const formData = new FormData()
this.selectedFile = event.target.files[0]
          formData.append('myFile', this.selectedFile, this.selectedFile.name)

          this.$http.post('http://127.0.0.1:8000/upload', formData).then(response => {

            console.log('onUpload file ', formData.length ); // 1   
            status =  response.status;
            console.log('status --> ', status)

            // get status text
            response.statusText;

          }, response => {
            // error callback
             console.log('errror ')
          });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question