antd?upload上傳如何獲取文件寬高
antd upload上傳獲取文件寬高
項(xiàng)目新加的需求,需要判斷上傳圖片的寬高,查了一下antd-upload組件貌似不支持這個(gè)查詢,因此需要使用外部的API
直接上代碼:beforeUpload 方法
handleBeforeUpload = async (file, fileList) => {
? ? ? ? const {fileMinSize = 0,fileMinWH,fileMaxWH, fileMaxSize = 50,uploadFormat = '',uploadFormatError = ''} = this.component.props;
? ? ? ? const isInRange = ((file.size > (fileMinSize * 1024 * 1024)) && (file.size < (fileMaxSize * 1024 * 1024)));
? ? ? ? let isTrueType = true,isFileWH = true;//類型,尺寸
? ? ? ? return new Promise((resolve, reject) =>{
? ? ? ? ? ? //校驗(yàn)格式
? ? ? ? ? ? if(uploadFormat != ''){
? ? ? ? ? ? ? ? let acceptArr = uploadFormat.split(',');
? ? ? ? ? ? ? ? let fileType = file.name.substring(file.name.lastIndexOf('.'));
? ? ? ? ? ? ? ? if(!acceptArr.includes(fileType)){
? ? ? ? ? ? ? ? ? ? isTrueType = false;
? ? ? ? ? ? ? ? ? ? this.message.error((uploadFormatError == '') ? '請(qǐng)上傳規(guī)則范圍內(nèi)的文件!' : uploadFormatError);
? ? ? ? ? ? ? ? ? ? this.base.ss({'data.fileList': fileList.pop()});
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //校驗(yàn)大小
?
? ? ? ? ? ? if (!isInRange) {//大小的標(biāo)識(shí)
? ? ? ? ? ? ? ? this.message.error((fileMaxSize == 50) ? '請(qǐng)上傳規(guī)則范圍內(nèi)的文件!' : '文件最大不能超過'+ fileMaxSize + 'M!');
? ? ? ? ? ? ? ? this.base.ss({'data.fileList': fileList.pop()});
? ? ? ? ? ? }
?
?
? ? ? ? ? ? //校驗(yàn)寬高
? ? ? ? ? ? /*********************************/
?
? ? ? ? ? ? if(fileMinWH && fileMaxWH){//做下過濾有的圖片需要過濾
? ? ? ? ? ? ? ? var reader = new FileReader();
? ? ? ? ? ? ? ? reader.readAsDataURL(file);
? ? ? ? ? ? ? ? reader.onload = (e) => {
? ? ? ? ? ? ? ? ? ? //加載圖片獲取圖片真實(shí)寬度和高度
? ? ? ? ? ? ? ? ? ? var image = new Image();
? ? ? ? ? ? ? ? ? ? image.src=reader.result;
? ? ? ? ? ? ? ? ? ? image.onload = () =>{
? ? ? ? ? ? ? ? ? ? ? ? var width = image.width;//圖片的寬
? ? ? ? ? ? ? ? ? ? ? ? var height = image.height;//圖片的高
? ? ? ? ? ? ? ? ? ? ? ? if(width < fileMinWH ?|| width > fileMaxWH || height < fileMinWH ?|| height > fileMaxWH){
? ? ? ? ? ? ? ? ? ? ? ? ? ? isFileWH = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.message.error('***寬高需要均大于600像素,小于4000像素');
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.base.ss({'data.fileList': []});
? ? ? ? ? ? ? ? ? ? ? ? ? ? reject();
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? resolve(isFileWH && isInRange && isTrueType)
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? /**********************************/
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? resolve(isInRange && isTrueType);
? ? ? ? ? ? }
? ? ? ? })
? ? };這樣這個(gè)功能就可以完美的解決了,*包著的代碼是最主要的。
antd上傳文件限制大小 react Hooks
const uploadImages = {
action: requestUrl + '/api/common/CommonUpload',
headers: {
SessionKey: `${localStorage.getItem('sk')}`,
},
data: (file) => {
return {
UploadType: 1027,//后端定義的type
Id: uuidv4(),
FileType: getUploadFileType(file),
};
},
beforeUpload: (file) => {// 禮品image
const limitFileNameLen = 100;
return new Promise((resolve, reject) => {
if (file.name && file.name.length > limitFileNameLen) {
message.error('Please upload a file with a file name less than 100 characters');
//請(qǐng)上傳文件名不超過100個(gè)字符的文件
return Promise.reject();
}
const limitM = 2;
const isLimit = file.size / 1024 / 1024 <= limitM;
console.log(isLimit);
if (!isLimit) {
message.error('The size exceeds the limit');
return Promise.reject();
}
return resolve();
});
},
}
模板:
?<Upload
? ? ? ? ? ? ? ? {...uploadImages}
? ? ? ? ? ? ? ? accept=".jpeg,.png,.jpg"
? ? ? ? ? ? ? ? listType="picture-card"
? ? ? ? ? ? ? ? fileList={fileList}
? ? ? ? ? ? ? ? onChange={handleChange}
? ? ? ? ? ? ? ? onPreview={handlePreview}
? ? ? ? ? ? >
? ? ? ? ? ? ? ? {fileList.length >= 4 ? null : uploadButton}
? ? ? ? ? ? </Upload>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser
這篇文章主要為大家介紹了解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
從零搭建react+ts組件庫(kù)(封裝antd)的詳細(xì)過程
這篇文章主要介紹了從零搭建react+ts組件庫(kù)(封裝antd),實(shí)際上,代碼開發(fā)過程中,還有很多可以輔助開發(fā)的模塊、流程,本文所搭建的整個(gè)項(xiàng)目,我都按照文章一步一步進(jìn)行了git提交,開發(fā)小伙伴可以邊閱讀文章邊對(duì)照git提交一步一步來(lái)看2022-05-05
React?跨端動(dòng)態(tài)化核心技術(shù)實(shí)例分析
這篇文章主要為大家介紹了React?跨端動(dòng)態(tài)化核心技術(shù)實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
react的ui庫(kù)antd中form表單使用SelectTree反顯問題及解決
這篇文章主要介紹了react的ui庫(kù)antd中form表單使用SelectTree反顯問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

