• 去掉img自带边框,前端文件下载,图片转base64发送后端


    1. 去掉img的自带边框,当src为空的时候不显示图片的默认边框:

    1. img[src=''] {
    2. opacity: 0;
    3. }

    2. 文件下载:

    1. export async function downLoadFile1(paramList, url) {
    2. let downloadParams = {
    3. data: JSON.stringify(paramList),
    4. }
    5. await axios({
    6. method: 'post',
    7. url: config.rootPath + url,
    8. data: qs.stringify(downloadParams),
    9. headers: {
    10. 'Content-Type': 'application/x-www-form-urlencoded', //将表单数据传递转化为form-data类型
    11. // 'Content-Type': 'application/json',
    12. },
    13. withCredentials: true,
    14. responseType: 'blob',
    15. // headers:{ 'Content-Type': 'application/json; application/octet-stream'},
    16. })
    17. .then((response) => {
    18. console.log(response)
    19. const { headers, data } = response
    20. // let fileName = headers['content-type']
    21. // ?.split('filename=')[1]
    22. // .split(';')[0]
    23. // console.log('ll',headers['content-type'].split('filename=')[1])
    24. // let fileName = headers['content-type'].split(';')[0]
    25. let fileName = 'file'
    26. const fileData = new Blob([data], { type: 'application/zip' })
    27. let file = window.URL.createObjectURL(fileData)
    28. let aHref = document.createElement('a')
    29. aHref.href = file
    30. aHref.download = decodeURIComponent(fileName)
    31. // aHref.download = 'uwenjain'
    32. aHref.click()
    33. window.URL.revokeObjectURL(file)
    34. console.log('scc1')
    35. // state.dialogState.open = false
    36. return false
    37. })
    38. .catch(() => {
    39. // state.dialogState.open = false
    40. console.log('error出错啦~文件不存在')
    41. // notice('error', '出错啦~', '文件不存在')
    42. })
    43. .finally(() => {
    44. return false
    45. })
    46. }

    3. 图片转base64:

    封装promise对象:

    1. //封装转化为base64的方法
    2. const changeBASE64 = file =>{
    3. return new Promise(resolve => {
    4. let fileReader = new FileReader()
    5. fileReader.readAsDataURL(file)
    6. fileReader.onload = ev =>{
    7. resolve(ev.target.result)
    8. }
    9. })
    10. }

    调用并使用:

    1. const beforeAvatarUpload = async (rawFile) => {
    2. let Base64
    3. Base64 = await changeBASE64(rawFile)
    4. console.log('baseliuyu',Base64)
    5. if (rawFile.type !== 'image/png' && rawFile.type !== 'image/jpeg') {
    6. ElMessage.error('请上传正确的图片格式!')
    7. return false
    8. } else if (rawFile.size >200000) {
    9. ElMessage.error('头像图片大小不能超过200KB!')
    10. return false
    11. }
    12. return true
    13. }

    formdata格式:

    1. const form = new FormData();
    2. form.append("file", rawFile);
    3. cmsInstance.post(config.rootPath + "/sys/sysTempFile/uploadFile", form, {
    4. headers: {
    5. "Content-Type": "multipart/form-data",
    6. },
    7. })
    8. //cmsInstance是axios的实例
    9. import axios from 'axios'
    10. export var cmsInstance = axios.create({
    11. baseURL: config.root, // 接口统一域名
    12. withCredentials: true, // 携带Cookie
    13. timeout: 60 * 1000, //设置超时
    14. })

  • 相关阅读:
    云原生--k8s一主多从架构搭建
    [02]Web前端进阶—script标签
    【持续更新】Spark Submit命令 配置参数详解
    优炫软件董事长梁继良当选新一届北京市商会副会长
    Electron 调试node_internal代码*
    企业微信应用开发实践
    Java设计模式三—— 策略模式、工厂模式
    RK3399平台开发系列讲解(USB篇)如何去学习USB驱动 - 视频课
    STK12与Python联合仿真(二):简单的例程
    设计WebHook
  • 原文地址:https://blog.csdn.net/qq_52179917/article/details/126455239