ldc-loue/resources/js/back/CkUploadAdaptater.js
Jérémy Munsch 0751114f4a
BO News, Tags, Sliders
CKeditor + word counter + Image Input + Taggable VUE3
Better Xss Sanitizer
Handle Video Youtube + Image Upload + Inline Image from url
Better Image Input component with reset, easizer resize
Full Responsive BO
2022-06-04 15:55:36 +02:00

56 lines
1.7 KiB
JavaScript

import axios from 'axios';
class CKUploadAdaptater {
constructor(loader) {
// The file loader instance to use during the upload.
this.loader = loader;
}
// Starts the upload process.
upload() {
return this.loader.file
.then(file => new Promise((resolve, reject) => {
this._initRequest();
this._sendRequest(file, resolve, reject);
}));
}
// Aborts the upload process.
abort() {
if (this.cancel) {
this.cancel.abort();
}
}
// Initializes the XMLHttpRequest object using the URL passed to the constructor.
_initRequest() {
this.cancel = axios.CancelToken.source();
this.route = require('./../modules/route').default.methods.route('crud-policies.upload')
}
// Prepares the data and sends the request.
_sendRequest(file, resolve, reject) {
// Prepare the form data.
const data = new FormData();
data.append('upload', file);
let headers = Object.assign({}, window.axios ? window.axios.defaults.headers.common : {})
headers['Content-Type'] = 'multipart/form-data'
axios.post(this.route, data, {
withCredentials: true,
headers: headers,
onUploadProgress: (evt) => {
this.loader.uploadTotal = evt.total;
this.loader.uploaded = evt.loaded;
}
}).then((response) => {
resolve({
default: response.data.url
})
}).catch((error) => {
console.error(error);
reject('Le fichier à été marbré')
})
}
}
export { CKUploadAdaptater }