• 去掉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. })

  • 相关阅读:
    Ubuntu20.04环境下MySQL8.0.30的 用户管理,设置修改密码,密码过期策略,权限管理,角色管理
    5个月做视频号的心路历程
    Essential C++ 编程基础
    Hadoop的第二个核心组件:MapReduce框架第二节
    网站的常见攻击与防护方法
    驱动开发:内核LDE64引擎计算汇编长度
    力扣:143. 重排链表(Python3)
    C++ 学习之旅(2.5)——变量与函数
    DB2存储过程如何编写和执行
    Python Http请求和HTML的解析
  • 原文地址:https://blog.csdn.net/qq_52179917/article/details/126455239