我的檔案顯示在控制臺中,但出現 400 錯誤,即請上傳檔案。
function AdminPanel() {
const [show, setShow] = useState(false);
const [pdffile, setPdffile] = useState("");
function handleFileChange(e) {
const files = e.target.files[0];
setPdffile(files);
console.log("Guru4666", pdffile);
}
const SubmitPDF = () => {
axios({
method: "post",
url: "url",
data: {"dataFile" : pdffile }
}).then(res => {
console.log("REs", res)
})
}
<div className="form-group form_aside">
<input type="file" className="form-control" id="files"
name="PDF" onChange={handleFileChange} />
</div>
提交
控制臺結果是
Guru4666
File {
lastModified: 1634196746000
lastModifiedDate: Thu Oct 14 2021 13:02:26
GMT 0530 (India Standard Time) {}
name: "barkelou.png"size: 5906type:
"image/png"webkitRelativePath: ""
[[Prototype]]: File}
API結果是:-
dataFile: {}
400
請幫忙,我需要緊急幫助。
uj5u.com熱心網友回復:
您可能應該使用FormData和multipart/form-data
const SubmitPDF = () => {
const formData = new FormData();
formData.append("dataFile", pdffile);
axios({
method: "post",
url: "url",
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res => {
console.log("REs", res)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/495680.html
標籤:javascript 反应 反应式 反应还原 事件处理
