[Vuejs]-Pass object from a vuex store action

0๐Ÿ‘

<input class="form-control block" type="file" ref="files" multiple required @change="handleFileUpload">

  data() {
    return {
      email: '',
      username: '',
      files: [],
      isSubmitting: false,
      isSubmitted: false,
      isError: false,
    };
  },
  methods: {
    handleFileUpload() {
      // eslint-disable-next-line
      this.files = this.$refs.files.files;
    },
    async addWork() {
      try {
        this.isSubmitting = true;
        this.isError = false;
        const formData = new FormData();
        for (let i = 0; i < this.files.length; i++) {
          const file = this.files[i];
          formData.append(`files['${i}']`, file);
        }
        const { username, email } = this;
        formData.append('email', email);
        formData.append('username', username);
        await WorksRepository.addWork(formData);
        this.isSubmitted = true;
      } catch (e) {
        this.isError = true;
      } finally {
        this.isSubmitting = false;
      }
    },

you can see full example here

Leave a comment