+ /**
+ * @param {Upload} upload
+ */
+ startXhrForUpload(upload) {
+ const formData = new FormData();
+ formData.append('file', upload.file, upload.file.name);
+
+ const req = window.$http.createXMLHttpRequest('POST', this.url, {
+ error() {
+ upload.markError('Upload failed'); // TODO - Update text
+ },
+ readystatechange() {
+ if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
+ upload.markSuccess('Finished upload!');
+ } 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 || content;
+ upload.markError(message);
+ }
+ },
+ });
+
+ req.upload.addEventListener('progress', evt => {
+ const percent = Math.min(Math.ceil((evt.loaded / evt.total) * 100), 100);
+ upload.updateProgress(percent);
+ });
+
+ req.setRequestHeader('Accept', 'application/json');
+ req.send(formData);
+ }
+