Last active
May 30, 2022 11:17
-
-
Save yeyuguo/f674bb97e5dae98679c76b0922bfad47 to your computer and use it in GitHub Desktop.
判断图片尺寸 和 大小
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 处理文件大小 | |
function handleSize(file, size) { | |
const isLimitSize = file.size / 1024 / 1024 < size | |
if (!isLimitSize) { | |
Message.error(`上传图片大小不能超过${size}M`) | |
return false | |
} | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**处理大小 | |
* @param {String} styleSize 样式宽高 'width x height' | |
* @param {Object} file elementUI 的file 对象 | |
*/ | |
function handleStyleSize(file, styleSize) { | |
return new Promise((resolve, reject) => { | |
if (!styleSize) return resolve() | |
let width = 0; | |
let height = 0; | |
[width, height] = styleSize.split('x').map(item => +item.trim()).filter(item => item) | |
if (!(width && height)) { | |
return reject('styleSize 格式不对,输入 "width x height"') | |
} | |
const _URL = window.URL || window.webkitURL; | |
const image = new Image(); | |
image.onload = () => { | |
const valid = image.width === width && image.height === height; | |
valid ? resolve() : reject(`图片尺寸限制为${width} x ${height}`); | |
}; | |
image.src = _URL.createObjectURL(file); | |
}).then( | |
() => { | |
return file; | |
}, | |
(err) => { | |
this.$message.error(err); | |
return Promise.reject(err); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment