Files
lucent-laravel/front/js/svelte/files/Uploader.svelte
T

67 lines
1.8 KiB
Svelte
Raw Normal View History

2023-10-02 23:10:49 +03:00
<script>
2026-04-29 19:40:37 +03:00
import { createEventDispatcher, getContext } from "svelte";
2023-10-02 23:10:49 +03:00
const dispatch = createEventDispatcher();
const channel = getContext("channel");
2026-04-29 19:40:37 +03:00
export let recordId;
2023-10-02 23:10:49 +03:00
let mimeTypes = "";
let files = [];
let isLoading = false;
// export function onUploadComplete(files){
// console.log(files)
// }
function upload(e) {
isLoading = true;
files = e.target.files ? [...e.target.files] : [];
let formData = new FormData();
2026-04-29 19:40:37 +03:00
formData.append("recordId", recordId);
2023-10-02 23:10:49 +03:00
Array.from(files).forEach(function (file) {
formData.append("files[]", file);
});
dispatch("beforeUpload", files);
2026-04-29 19:40:37 +03:00
fetch(channel.lucentUrl + "/files/upload", {
method: "POST",
body: formData,
headers: {
"X-CSRF-TOKEN": document.querySelector(
'meta[name="csrf-token"]',
).content,
},
})
.then((response) => response.json())
.then((data) => {
if (data.error) {
dispatch("uploadError", data.error);
2023-10-02 23:10:49 +03:00
} else {
2026-04-29 19:40:37 +03:00
dispatch("uploadComplete", data);
2023-10-02 23:10:49 +03:00
}
isLoading = false;
});
}
</script>
2026-04-29 19:40:37 +03:00
<fieldset class="upload-button" disabled={isLoading}>
<label class="button primary btn-spinner">
<span
class="spinner-border spinner-border-sm"
role="status"
aria-hidden="true"
/>
2023-10-02 23:10:49 +03:00
Upload file
2024-08-16 14:34:39 +03:00
2023-10-02 23:10:49 +03:00
<input
on:input={upload}
class="form-control"
type="file"
id="formFile"
multiple
accept={mimeTypes}
disabled={isLoading}
hidden
/>
</label>
</fieldset>