antd upload控件的data參數(shù)使用
antd upload控件的data參數(shù)
通過data屬性傳遞的參數(shù),在后臺可以通過request獲取到。
經(jīng)測試
data可以是一個對象或者函數(shù) ,
?<
?Upload?
?name=
?"file"?
?data=
?{this.
?state.
?ingoreFirst
?}?
?action=
?"/api/uploadFile"?
?onChange=
?{(
?info)
?=>this.
?handleUploadFileOnChange(
?info)
?}?
?>如果是一個bool值,則request.fields獲取到空{(diào)}
這樣就可以
?<
?Upload?
?name=
?"file"?
?data=
?{this.
?state
?}?
?action=
?"/api/uploadFile"?
?onChange=
?{(
?info)
?=>this.
?handleUploadFileOnChange(
?info)
?}?
?>如果不想把整個state返回服務(wù)器端
可以用函數(shù)
?<
?Upload?
?name=
?"file"?
?data=
?{()
?=>this.
?handleUploadData()
?}?
?action=
?"/api/uploadFile"?
?onChange=
?{(
?info)
?=>this.
?handleUploadFileOnChange(
?info)
?}?
?>? handleUploadData()?
? ?{?
?
? let?
? d={
? ingoreFirst:
? this.
? state.
? ingoreFirst,
? ingoreLast:
? this.
? state.
? ingoreLast}?
? return?
? d;
? ?}antd upload組件使用
項目場景
- 使用背景:上傳圖片給服務(wù)器并且需要額外的傳遞參數(shù)
- 使用antd組件庫,form表單下的upload組件
使用
圖片上傳自定義方法的使用,參數(shù)的上傳
其中customRequest 作為自定義上傳的方法與后端進行交互并可以傳遞額外的參數(shù)
beforeUpload 方法可以對上傳的圖片類型和大小做一下相對簡單的前端校驗
<Upload
name="uploadFile"
listType="picture-card"
className="avatar-uploader"
showUploadList={false}
fileList={fileList}
customRequest={this.uploadHeadImg} //自定義上傳的方法
beforeUpload={this.beforeUpload}
onChange={this.handleChange}
>
{imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
</Upload>```
uploadHeadImg =(option) => {
const { pageTenantId } = this.state
const formdata= new FormData();
formdata.append('pageTenantId',pageTenantId);
formdata.append('uploadFileName',option.file.name);
formdata.append('uploadFile', option.file)
axios.post('后端提供的接口',formdata,{headers:{
"Content-Type": "application/x-www-form-urlencoded"
}}).then(res=>{
if(res.data.status==0) {
option.onSuccess(res.data.data.returnParams.fileUrl)
this.setState({
logoUrl: res.data.data.returnParams.fileUrl
})
}
}
)
}
onchange方法可以通過上傳的狀態(tài)對文件進行一些判斷
handleChange = async(info) => {
if (info.file.status === 'uploading') {
this.setState({ loading: true });
return;
}
if (info.file.status === 'done') {
// Get this url from response in real world.
await this.getBase64(info.file.originFileObj, imageUrl =>
this.setState({
imageUrl,
loading: false,
fileList:info.fileList,
}),
);
}
};
beforeUpload的具體使用根據(jù)要求進行判斷
beforeUpload(file) {
// 只允許圖片的jpeg和png類型
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
message.error('圖片類型只能為JPEG/PNG!');
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
message.error('圖片不能大于2MB!');
}
return isJpgOrPng && isLt2M;
}
在form表單中要使用getValueFromEvent對上傳的文件數(shù)據(jù)賦值具體使用

normFile 方法把文件return出來
normFile = (e) => {
this.setState({
uploadFileName:e.file.name
})
if (Array.isArray(e)) {
return e;
}
return e && e.fileList;
};
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
js前端實現(xiàn)多圖圖片上傳預(yù)覽的兩個方法(推薦)
下面小編就為大家?guī)硪黄猨s前端實現(xiàn)多圖圖片上傳預(yù)覽的兩個方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
Bootstrap modal只加載一次數(shù)據(jù)的解決辦法(推薦)
這篇文章主要介紹了Bootstrap modal只加載一次數(shù)據(jù)的解決辦法,以及bootstrap模態(tài)框的簡單使用,需要的朋友可以參考下2017-11-11
JS實現(xiàn)iframe自適應(yīng)高度的方法(兼容IE與FireFox)
這篇文章主要介紹了JS實現(xiàn)iframe自適應(yīng)高度的方法,涉及javascript與iframe交互動態(tài)操作頁面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-06-06
前端使用xlsx導(dǎo)出數(shù)據(jù)生成Excel文件的全過程
這篇文章主要給大家介紹了關(guān)于前端使用xlsx導(dǎo)出數(shù)據(jù)生成Excel文件的相關(guān)資料,最近在做項目中,后端偷懶不做導(dǎo)出功能,讓我前端實現(xiàn),所以在這里記錄下前端導(dǎo)出功能,需要的朋友可以參考下2023-08-08

