fix(upload): auto-upload on drag-drop and fix FormData serialization
This commit is contained in:
parent
e2e1723b99
commit
70d8bcb4d2
|
|
@ -1,6 +1,7 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed, watch } from 'vue'
|
||||||
import { router } from '@inertiajs/vue3'
|
import { router } from '@inertiajs/vue3'
|
||||||
|
import axios from 'axios'
|
||||||
import Vue3Dropzone from '@jaxtheprime/vue3-dropzone'
|
import Vue3Dropzone from '@jaxtheprime/vue3-dropzone'
|
||||||
import '@jaxtheprime/vue3-dropzone/dist/style.css'
|
import '@jaxtheprime/vue3-dropzone/dist/style.css'
|
||||||
|
|
||||||
|
|
@ -44,6 +45,12 @@ const acceptedExtensions = ['.png', '.jpg', '.jpeg', '.ppt', '.pptx', '.zip']
|
||||||
const isUploading = computed(() => uploading.value)
|
const isUploading = computed(() => uploading.value)
|
||||||
const progressPercent = computed(() => Math.round(uploadProgress.value))
|
const progressPercent = computed(() => Math.round(uploadProgress.value))
|
||||||
|
|
||||||
|
watch(files, (newFiles) => {
|
||||||
|
if (newFiles.length > 0 && !uploading.value) {
|
||||||
|
processFiles()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function processFiles() {
|
function processFiles() {
|
||||||
if (files.value.length === 0) return
|
if (files.value.length === 0) return
|
||||||
|
|
||||||
|
|
@ -63,6 +70,9 @@ function uploadNextFile(index) {
|
||||||
files.value = []
|
files.value = []
|
||||||
emit('uploaded')
|
emit('uploaded')
|
||||||
|
|
||||||
|
// Reload Inertia page data to reflect newly uploaded slides
|
||||||
|
router.reload({ preserveScroll: true })
|
||||||
|
|
||||||
// Reset progress after brief display
|
// Reset progress after brief display
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
uploadProgress.value = 0
|
uploadProgress.value = 0
|
||||||
|
|
@ -95,25 +105,22 @@ function uploadNextFile(index) {
|
||||||
formData.append('expire_date', expireDate.value)
|
formData.append('expire_date', expireDate.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
router.post(route('slides.store'), formData, {
|
axios.post(route('slides.store'), formData, {
|
||||||
forceFormData: true,
|
headers: { 'Content-Type': 'multipart/form-data' },
|
||||||
preserveScroll: true,
|
onUploadProgress: (event) => {
|
||||||
preserveState: true,
|
if (event.total) {
|
||||||
onProgress: (event) => {
|
const percent = Math.round((event.loaded / event.total) * 100)
|
||||||
if (event.percentage) {
|
|
||||||
// Per-file progress weighted across total
|
|
||||||
const perFileWeight = 100 / totalCount.value
|
const perFileWeight = 100 / totalCount.value
|
||||||
uploadProgress.value = (index * perFileWeight) + (event.percentage / 100 * perFileWeight)
|
uploadProgress.value = (index * perFileWeight) + (percent / 100 * perFileWeight)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
}).then(() => {
|
||||||
uploadedCount.value = index + 1
|
uploadedCount.value = index + 1
|
||||||
uploadNextFile(index + 1)
|
uploadNextFile(index + 1)
|
||||||
},
|
}).catch((error) => {
|
||||||
onError: (errors) => {
|
uploading.value = false
|
||||||
uploading.value = false
|
const errors = error.response?.data?.errors
|
||||||
uploadError.value = errors.file?.[0] || errors.message || 'Upload fehlgeschlagen.'
|
uploadError.value = errors?.file?.[0] || error.response?.data?.message || 'Upload fehlgeschlagen.'
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue