• 请求二进制数据和base64格式数据的预览显示


    1、请求二进制数据,各种格式的数据预览显示。

    1. axios({
    2. method: 'get',
    3. url: `getFile/${type}`,
    4. headers: {
    5. Authorization: userStore.token,
    6. },
    7. // 这里是必须的 'blob'
    8. responseType: 'blob',
    9. }).then((res: any) => {
    10. // pdf 格式
    11. if (type == 1) {
    12. let blob = new Blob([res.data], { type: 'application/pdf' });
    13. pdfSrc.value = URL.createObjectURL(blob);
    14. window.open(pdfSrc.value);
    15. } else if (type == 2) {
    16. // 图片 jpeg格式
    17. let blob = new Blob([res.data], { type: 'image/jpeg' });
    18. imgSrc.value = window.URL.createObjectURL(blob);
    19. } else if (type == 3) {
    20. // 音频
    21. let blob = new Blob([res.data], { type: 'wav/audio' });
    22. if (audio.value) {
    23. audio.value.src = window.URL.createObjectURL(blob);
    24. audio.value.play();
    25. }
    26. } else if (type == 4) {
    27. // 图片 excel格式
    28. const link = document.createElement('a');
    29. let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
    30. link.style.display = 'none';
    31. link.href = URL.createObjectURL(blob); //创建一个指向该参数对象的URL
    32. link.setAttribute('download', `表资源${new Date().getTime()}.xls`);
    33. document.body.appendChild(link);
    34. link.click(); //触发下载
    35. document.body.removeChild(link);
    36. }
    37. });

    注意点:

    A、要把 responseType 设置为 ‘blob’; 

    B、要把对应的type设置好,比如图片 new Blob([res.data], { type: 'image/jpeg' })。

    2、base64格式数据图片预览显示

    1. getOperImg()
    2. .then((res) => {
    3. if (res) {
    4. const { code, data } = res;
    5. if (code == 200) {
    6. if (Array.isArray(data) && data.length != 0) {
    7. const { operPic } = data[0];
    8. // operPic 就是请求来的base64图片数据
    9. // 最终的图片地址 需要前边拼接 data:image/jpg;base64,
    10. imgSrc.value = `data:image/jpg;base64,${operPic}`;
    11. }
    12. }
    13. }
    14. })
    15. .catch((err) => {
    16. console.log('获取图片', err);
    17. });

  • 相关阅读:
    python2安装biopython
    今年最后一次PMP考试!想升职加薪抓紧!
    前端demo: 实现对图片进行上传前的压缩功能
    【我的创作纪念日】使用pix2pixgan实现barts2020数据集的处理(完整版本)
    【Hadoop---13】MapReduce:Shuffle『Partitioner | Combiner』
    Web端即时通讯技术:WebSocket、socket.io、SSE
    amd64
    SQL优化
    思考的技术- 读书笔记
    YOLOv5、YOLOv8改进:Swin Transformer-V2
  • 原文地址:https://blog.csdn.net/jmszl1991/article/details/125482409