X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/607da7310925087bba93fd5b178739e5a2be7c66..refs/pull/5400/head:/resources/js/components/dropzone.js diff --git a/resources/js/components/dropzone.js b/resources/js/components/dropzone.js index 1fdf824ae..598e0d8d4 100644 --- a/resources/js/components/dropzone.js +++ b/resources/js/components/dropzone.js @@ -1,8 +1,8 @@ import {Component} from './component'; -import {Clipboard} from '../services/clipboard'; +import {Clipboard} from '../services/clipboard.ts'; import { elem, getLoading, onSelect, removeLoading, -} from '../services/dom'; +} from '../services/dom.ts'; export class Dropzone extends Component { @@ -15,12 +15,14 @@ export class Dropzone extends Component { this.isActive = true; this.url = this.$opts.url; + this.method = (this.$opts.method || 'post').toUpperCase(); this.successMessage = this.$opts.successMessage; this.errorMessage = this.$opts.errorMessage; this.uploadLimitMb = Number(this.$opts.uploadLimit); this.uploadLimitMessage = this.$opts.uploadLimitMessage; this.zoneText = this.$opts.zoneText; this.fileAcceptTypes = this.$opts.fileAccept; + this.allowMultiple = this.$opts.allowMultiple === 'true'; this.setupListeners(); } @@ -83,7 +85,12 @@ export class Dropzone extends Component { } manualSelectHandler() { - const input = elem('input', {type: 'file', style: 'left: -400px; visibility: hidden; position: fixed;', accept: this.fileAcceptTypes}); + const input = elem('input', { + type: 'file', + style: 'left: -400px; visibility: hidden; position: fixed;', + accept: this.fileAcceptTypes, + multiple: this.allowMultiple ? '' : null, + }); this.container.append(input); input.click(); input.addEventListener('change', () => { @@ -161,6 +168,9 @@ export class Dropzone extends Component { startXhrForUpload(upload) { const formData = new FormData(); formData.append('file', upload.file, upload.file.name); + if (this.method !== 'POST') { + formData.append('_method', this.method); + } const component = this; const req = window.$http.createXMLHttpRequest('POST', this.url, { @@ -171,10 +181,7 @@ export class Dropzone extends Component { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { upload.markSuccess(component.successMessage); } else if (this.readyState === XMLHttpRequest.DONE && this.status >= 400) { - const content = this.responseText; - const data = content.startsWith('{') ? JSON.parse(content) : {message: content}; - const message = data?.message || data?.error || content; - upload.markError(message); + upload.markError(window.$http.formatErrorResponseText(this.responseText)); } }, });