網站首頁 編程語言 正文
注意:文件上傳,要不要存在阿里云,要不要永久保存,回顯的時候 需要阿里云,用完就刪除的直接給后端就行,不需要存在阿里云。
1、原生input提交
/** 提交校驗 */
onCheck = async () => {
const { idType, defaultValue, getSubmitValue } = this.props;
const { selectCode } = this.state;
try {
// 方法一:
// const formDataObject = new FormData();
// console.log(($("#file")[0] as any).files, "所有文件")見下面截圖
// const files = ($("#file")[0] as any).files;
// 同時上傳多個文件時的寫法
// for (const file of files) {
// formDataObject.append('files', file);
// }
// 方法二:
const input = document.querySelector("input[type='file']");
//const input = document.querySelector("input[type='file']");
// 等同于
//const input = document.querySelector(".input");
const formDataObject = new FormData(); // FormData 對象獲取文件數據
// 同時上傳多個文件時的寫法
for (let i = 0; i < (input as any).files?.length; i++) {
formDataObject.append("file", (input as any).files[i]);
}
// 方法三
const values = (await this.props.form.validateFields()) as unknown as TFarmLand;
values.deptId = !isEmpty(selectCode) ? selectCode?.map(Number)[selectCode.length - 1] : defaultValue?.deptId as number;
const data = new FormData();
const formDataObject = idType === "edit" ? data : formData;
formDataObject.append("name", values.name || "");
await tFarmLandProvider.addFarmForm(formDataObject).then(async (res) => {
if (res.code === 200) {
this.props.getSubmitValue && this.props.getSubmitValue(true);
message.success("新增成功!");
}
return;
}).catch(err => {
this.props.getSubmitValue && this.props.getSubmitValue(false);
message.error(err.response?.data?.msg || "新增失敗!");
return;
});
} catch (errorInfo) {
console.log(errorInfo, "errorInfo")
message.warn("提交校驗失敗!");
return null;
}
}
// 方法三
/** 獲取formdata */
getBeforeUpload = (file: RcFile) => {
console.log(file, "file")
const data = new FormData();
data.append("file", file);
this.setState({
formData: data,
})
return false;
}
render() {
return (
// 方法一:
// <div id="uploadForm">
// <input id="file" type="file" name="file" multiple/>
// </div>
// 方法二:
//<form id="form_upload" encType="multipart/form-data">
// <input className="input" type="file" name="file" multiple />
//</form>
// 方法三:
<Form className="farm">
<Form.Item label="農田名稱">
{getFieldDecorator("name", {
initialValue: defaultValue?.name,
})(
<Input
placeholder="請輸入"
/>)}
</Form.Item>
{<Form.Item label="農田文件上傳" extra="請上傳農田shp格式文件壓縮包" style={{ display: idType === "edit" ? "none" : "" }}>
{getFieldDecorator("file", {
validateTrigger: "onSubmit",
rules: [
{
required: idType === "edit" ? false : true,
message: "文件必須上傳!",
},
{ validator: checkedUploadSuccess },
],
valuePropName: "file",
getValueFromEvent: (e: UploadChangeParam) => {
return e.fileList;
},
})(<Upload
name="file"
multiple
transformFile={transformFile}
accept=".zip"
beforeUpload={(file) => this.getBeforeUpload(file)}
listType="picture"
>
<Button>點擊上傳</Button>
</Upload>)}
</Form.Item>}
</Form>
)
}
?同時上傳多個的時候,就會并列給2個file: binary
?所有文件打印:
原文鏈接:https://blog.csdn.net/weixin_45481456/article/details/131474837
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-13 beginInvke帶回調函數使用
- 2022-03-12 c++調用實現yolov5轉onnx介紹_C 語言
- 2022-08-21 caffe的python接口生成配置文件學習_python
- 2022-12-24 c++類成員函數如何做函數參數_C 語言
- 2023-01-21 RecyclerBezierChart曲線圖表繪制_Android
- 2024-02-01 idea設置格式化豎線
- 2022-03-07 C++中簡單的文本文件輸入/輸出示例詳解_C 語言
- 2022-08-01 C語言深入探索遞歸的特點_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支