1.加multipart/form-data
//用户端图片上传 加multipart/form-data
export const uploadImage = (params) => {
return $.post('/app/pat/pushpull/imgUpload', params, { headers: { "Content-Type": "multipart/form-data" } })
}
2.还有最主要用 new FormData 来传。。。。
async getImageByHost (file, field) {
console.log(111, file)
let File = new FormData()
File.append("file", file)
File.append("type", "Copy")
const [err, res] = await to(uploadImage(File))
if (err) return this.$toast(err.errorMsg)
console.log(2222, res)
if (!res) {
this[field] = []
}
return res
}
//上传前通用限制
function beforeUpload(file: any) {
// 限制图片 格式、size、分辨率
const isJPG = file.type === "image/jpeg"
const isJPEG = file.type === "image/jpeg"
const isGIF = file.type === "image/gif"
const isPNG = file.type === "image/png"
const isLt2M = file.size / 1024 / 1024 < 2
if (!(isJPG || isJPEG || isPNG)) {
Modal.error({
title: "只能上传JPG、JPEG、PNG格式的图片~",
})
} else if (!isLt2M) {
Modal.error({
title: "图片超过2M限制,不允许上传~",
})
}
return (isJPG || isJPEG || isPNG) && isLt2M
}