110 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Uppy, Dashboard, XHRUpload } from "https://releases.transloadit.com/uppy/v3.15.0/uppy.min.mjs";
const uploadEndpoint = window.crmActivitiesUploadEndpoint ?? "/activities/upload";
// var uppy = new Uppy({
// onBeforeFileAdded: (currentFile, files) => {
// //const name = Date.now() + '_' + currentFile.name
// const month = (new Date().getMonth() + 1).toString().padStart(2, '0');
// const day = (new Date().getDate()).toString().padStart(2, '0');
// const name = `${new Date().getFullYear()}_${month}_${day}_${currentFile.name}`;
// console.log(name);
// const modifiedFile = {
// ...currentFile,
// meta: {
// ...currentFile.meta,
// name
// },
// name
// };
// return modifiedFile
// }})
var uppy = new Uppy({
onBeforeFileAdded: (currentFile, files) => {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
// Menghilangkan koma (,) dalam nama file
const sanitizedFileName = currentFile.name.replace(/,/g, '');
// Format: HHMMSSmmm_FILENAME
const name = `${hours}${minutes}${seconds}${milliseconds}_${sanitizedFileName}`;
// Membuat objek file yang dimodifikasi
const modifiedFile = {
...currentFile,
meta: {
...currentFile.meta,
name
},
name
};
return modifiedFile;
}
})
.use(Dashboard, {
inline: true,
target: "#drag-drop-area",
width: '100%',
height: 300,
})
.use(XHRUpload, { endpoint: uploadEndpoint, method: 'post' });
uppy.on("complete", (result) => {
let array = result.successful;
let arrtext = [];
for (let i = 0; i < array.length; i++) {
let text = result.successful[i].name;
if (result.successful[i].response && result.successful[i].response.body && result.successful[i].response.body.relativePath) {
text = result.successful[i].response.body.relativePath;
}
arrtext.push(text);
}
let text = arrtext.join();
$('#attachment').val(function(){
if (this.value == '') {
return this.value + text;
} else if (this.value != ''){
return this.value + ',' + text;
}
});
console.log( "Upload complete! Weve uploaded these files:", result.successful);
});
/*
const attachmentList = document.querySelector('#attachment').value;
const arrayx = attachmentList ? attachmentList.split(',') : [];
arrayx.forEach((fileName) => {
fetch(`<?=base_url();?>/upload/${fileName}`)
.then((response) => response.blob())
.then((blob) => {
uppy.addFile({
name: fileName.substring(11),
type: blob.type,
data: blob,
});
if (fileName === arrayx[arrayx.length - 1]) {
uppy.getFiles().forEach((file) => {
uppy.setFileState(file.id, {
progress: { uploadComplete: true, uploadStarted: true },
});
});
}
});
});
*/