Last change
on this file since 07f4e8b was e6c2521, checked in by darsov2 <62809499+darsov2@…>, 10 months ago |
images upload/download impl, other fixes
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | import axios from "../../../axios";
|
---|
2 | import { useState } from "react";
|
---|
3 |
|
---|
4 | const useFileChange = () => {
|
---|
5 | const [selectedFiles, setSelectedFiles] = useState([]);
|
---|
6 |
|
---|
7 | const onFileChangeHandler = (files, type, id, refresh) => {
|
---|
8 | setSelectedFiles(files);
|
---|
9 |
|
---|
10 | const formData = new FormData();
|
---|
11 |
|
---|
12 | files.forEach((x) => formData.append('files', x))
|
---|
13 | //
|
---|
14 | // for (let i = 0; i < files.length; i++) {
|
---|
15 | //
|
---|
16 | // }
|
---|
17 |
|
---|
18 |
|
---|
19 | axios.post(`/upload/${type}/${id}`, formData, {
|
---|
20 | headers: {
|
---|
21 | 'Content-Type': 'multipart/form-data'
|
---|
22 | }
|
---|
23 | })
|
---|
24 | .then(res => {
|
---|
25 | if (res.status === 200) {
|
---|
26 | console.log(res.data);
|
---|
27 | refresh((prev) => ++prev)
|
---|
28 | alert("File(s) uploaded successfully.");
|
---|
29 | } else {
|
---|
30 | console.error("File upload failed:", res.data);
|
---|
31 | alert("File upload failed.");
|
---|
32 | }
|
---|
33 | })
|
---|
34 | .catch(error => {
|
---|
35 | console.error("Error uploading file(s):", error);
|
---|
36 | alert("An error occurred while uploading files.");
|
---|
37 | });
|
---|
38 | };
|
---|
39 |
|
---|
40 | return { onFileChangeHandler, selectedFiles };
|
---|
41 | };
|
---|
42 |
|
---|
43 | export default useFileChange;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.