I am using react-use-file-upload.
Given below is the doc example:
import React, { useRef } from 'react';
import axios from 'axios';
import useFileUpload from 'react-use-file-upload';
const Upload = () => {
const {
files,
fileNames,
fileTypes,
totalSize,
totalSizeInBytes,
handleDragDropEvent,
clearAllFiles,
createFormData,
setFiles,
removeFile,
} = useFileUpload();
const inputRef = useRef();
const handleSubmit = async (e) => {
e.preventDefault();
const formData = createFormData();
try {
axios.post('https://some-api.com', formData, {
'content-type': 'multipart/form-data',
});
} catch (error) {
console.error('Failed to submit files.');
}
};
return (
<div css={CSS}>
<h1>Upload Files</h1>
<p>Please use the form to your right to upload any file(s) of your choosing.</p>
<div className="form-container">
{/* Display the files to be uploaded */}
<div css={ListCSS}>
<ul>
{fileNames.map((name) => (
<li key={name}>
<span>{name}</span>
<span onClick={() => removeFile(name)}>
<i className="fa fa-times" />
</span>
</li>
))}
</ul>
{files.length > 0 && (
<ul>
<li>File types found: {fileTypes.join(', ')}</li>
<li>Total Size: {totalSize}</li>
<li>Total Bytes: {totalSizeInBytes}</li>
<li className="clear-all">
<button onClick={() => clearAllFiles()}>Clear All</button>
</li>
</ul>
)}
</div>
{/* Provide a drop zone and an alternative button inside it to upload files. */}
<div
css={DropzoneCSS}
onDragEnter={handleDragDropEvent}
onDragOver={handleDragDropEvent}
onDrop={(e) => {
handleDragDropEvent(e);
setFiles(e, 'a');
}}
>
<p>Drag and drop files here</p>
<button onClick={() => inputRef.current.click()}>Or select files to upload</button>
{/* Hide the crappy looking default HTML input */}
<input
ref={inputRef}
type="file"
multiple
style={{ display: 'none' }}
onChange={(e) => {
setFiles(e, 'a');
inputRef.current.value = null;
}}
/>
</div>
</div>
<div className="submit">
<button onClick={handleSubmit}>Submit</button>
</div>
</div>
);
};
But the problem is, currently in my component, I have more than one input type file.
So, I am confused, how can I manage all my file inputs by using this library.
If anyone already did, please help!
Note: I want feature like :
<input />
<input />
.
.
.
all inputs are of type file.