在 React js 中,我只想知道文件名,所以我只是简单地使用文件目标函数
import React, { useState } from "react";
export default function App() {
const [filename, setFileName] = useState([]);
const handle = (event) => {
const data = [];
for (let i = 0; i < event.target.files.length; i++) {
data.push(event.target.files[i]);
}
console.log("data :- ", data);
setFileName(data);
};
return (
<div className="App">
<h1>Hello...</h1>
<input type="file" onChange={handle} multiple />
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">File name</th>
</tr>
</thead>
{fileName.map((item, i) => {
return (
<React.Fragment key={i}>
<tbody>
<tr>
<th scope="row">{i}</th>
<td>{item.name}</td>
</tr>
</tbody>
</React.Fragment>
);
})}
</table>
</div>
);
}