Exif.js在Vuex上分派动作的问题

时间:2018-12-18 21:42:36

标签: callback promise vuex dispatch exif-js

我试图将EXIF.js与Vuex一起使用,以将图片的纬度,经度和日期发送到后端,但是我在分派动作时遇到问题。 问题在于EXIF.getData具有回调(因此无法在该处使用分派),而且如果我尝试在Promise中分派(如下面的代码),则不可能。 您认为有解决方案吗?

upload() {
                var formData = new FormData();
                let counter=0
                const formPromise = new Promise((resolve, reject) => {
                    let size = this.files.length
                    this.files.forEach(file => {
                        formData.append('images[]', file);
                        EXIF.getData(file, function () {
                            const long = EXIF.getTag(file, "GPSLongitude")
                            this.longitude = long[0].numerator + long[1].numerator /
                                    (60 * long[1].denominator) + long[2].numerator / (3600 * long[2].denominator)
                            const lat = EXIF.getTag(file, "GPSLatitude")
                            this.latitude = lat[0].numerator + lat[1].numerator /
                                    (60 * lat[1].denominator) + lat[2].numerator / (3600 * lat[2].denominator)
                            this.date = EXIF.getTag(file, "GPSDateStamp")
                            formData.append('latitudes[]', this.latitude);
                            formData.append('longitudes[]', this.longitude);
                            formData.append('dates[]', this.date);
                            counter+=1
                            if(counter == size) {
                                resolve(formData)
                            }
                        })
                    });
                })
            formPromise.then(function(formData){
                this.$store.dispatch('createPhotos', data)
            })
                const categoryId = this.categoryId
                const data = { formData, categoryId }
                this.snackbar = true
                this.files = [];
                this.images = [];
        }

0 个答案:

没有答案