javascript - React.js:使用 material-ui-dropzone 从文件中附加值时,formData 没有值
问题描述
我正在尝试使用名为的库来实现上传功能material-ui-dropzone
我认为上传文件工作正常,但是,当我使用 axios 发送 POST 请求时,formData
即使在发送请求之前,我总是会得到一个空的
向内.js
import React, { useState, useEffect, forwardRef } from "react";
import useStyles from "./styles";
import { Paper, Button, Grid } from "@material-ui/core";
import { DropzoneDialog } from "material-ui-dropzone";
import MaterialTable from "material-table";
import {
AddBox,
ArrowDownward,
Check,
ChevronLeft,
ChevronRight,
Clear,
DeleteOutline,
Edit,
FilterList,
FirstPage,
LastPage,
Remove,
SaveAlt,
Search,
ViewColumn,
Publish,
} from "@material-ui/icons";
import InwardService from "../../services/InwardService";
export default function Inward() {
const tableIcons = {
Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
DetailPanel: forwardRef((props, ref) => (
<ChevronRight {...props} ref={ref} />
)),
Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
PreviousPage: forwardRef((props, ref) => (
<ChevronLeft {...props} ref={ref} />
)),
ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
SortArrow: forwardRef((props, ref) => (
<ArrowDownward {...props} ref={ref} />
)),
ThirdStateCheck: forwardRef((props, ref) => (
<Remove {...props} ref={ref} />
)),
ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />),
};
var classes = useStyles();
const [isDropzoneOpen, setIsDropzoneOpen] = useState(false);
const [files, setFiles] = useState([]);
var formData = new FormData();
useEffect(() => {
if (files.length > 0) {
const newFilesMap = files.map(fileMap => {
formData.append("file", fileMap);
});
InwardService.uploadPesonetFile(newFilesMap);
}
}, [files]);
return (
<>
<Paper classes={{ root: classes.paperRoot }}>
<MaterialTable icons={tableIcons} title="" />
<Grid item xs={3}>
<br />
<Button
variant="contained"
color="default"
className={classes.button}
startIcon={<Publish />}
onClick={() => setIsDropzoneOpen(true)}
>
Upload File
</Button>
</Grid>
<DropzoneDialog
acceptedFiles={[".inc"]}
cancelButtonText={"cancel"}
submitButtonText={"submit"}
maxFileSize={5000000}
open={isDropzoneOpen}
onClose={() => setIsDropzoneOpen(false)}
onSave={(files) => {
setFiles(files);
setIsDropzoneOpen(false);
}}
showPreviews={true}
showFileNamesInPreview={true}
/>
</Paper>
</>
);
}
向内服务.js
import { Component } from "react";
import axios from "axios";
import { API_BASE_URL_UPLOAD } from "../constants/APIUtils";
class InwardService extends Component {
uploadPesonetFile(files) {
return axios.post(`${API_BASE_URL_UPLOAD}`, files, {headers: { 'Content-Type': 'multipart/form-data' }})
}
}
export default new InwardService();
好像这条线没有生效:
formData.append("file", fileMap);
formData
知道为什么即使在将文件/文件映射附加到其中之后我也没有任何价值?
解决方案
你应该传递formData
给uploadPesonetFile
in InwardService
,你正在传递newFilesMap
所以代码应该是:
files.map(fileMap => {
formData.append("file", fileMap);
});
InwardService.uploadPesonetFile(formData);
代替 :
const newFilesMap = files.map(fileMap => {
formData.append("file", fileMap);
});
InwardService.uploadPesonetFile(newFilesMap);
推荐阅读
- javascript - v8::HandleScope::CreateHandle() 中的致命错误 # 无法在没有 HandleScope 的情况下创建句柄
- java - 在运行时更改休眠 JPA 属性
- google-chrome-extension - 有没有办法使用 Airwatch 安装浏览器扩展程序?
- python - 尝试使用当前时间和经过的时间计算在 Python 中输入的时间
- flutter - Dart 程序执行不按顺序
- android-studio-4.0 - Android Studio 4.0 升级后“无法识别启动活动:未找到默认活动”
- python - 在 Python 中合并 2 个数组
- xaml - 基于 Itemsource {Binding} 到 ObservableCollection 的动态 ListView 显示<"这可以改变和规定 listview 样式"?>
- python - 根据分类值对列进行编码
- environment-variables - CircleCI 插入环境变量