当前位置: 首页 > 知识库问答 >
问题:

javascript - 如何自定义文件下载,可以自定义文件名?

陆洛城
2025-03-09

文件已经处理过跨域

共有2个答案

陈畅
2025-03-09

文件流下载的可以
export const fileDownload = (id) => get(/file/download/${id}, { responseType: 'blob' })

async handleDown(id) {
  this.loadingText = '正在下载,请稍候'
  this.loading = true
  try {
    let res = await fileDownload(id)

    console.log(res.data, 'res.data')
    const blob = new Blob([res.data])
    const reader = new FileReader()
    reader.readAsDataURL(blob)
    reader.onload = e => {
      const filename = res.headers['content-disposition'].split('filename=')[1]  //或者可以改成自定义的文件名称
      const a = document.createElement('a')
      a.download = decodeURIComponent(filename)
      a.href = e.target.result
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
    }
    this.loading = false
    this.$message.success('文件导出成功')
  } catch (e) {
    throw new Error(`下载文件 ${item.name} 失败`)
  }
},
夏侯野
2025-03-09

使用a标签自定义文件名有跨域限制,还有浏览器兼容性问题

import {saveAs} from 'file-saver';

export const fileType = () => {
    return {
        pdf: 'application/pdf',
        docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        xls: 'application/vnd.ms-excel',
        doc: 'application/msword',
        ppt: 'application/vnd.ms-powerpoint',
        pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    };
};


/**
 * @description 根据url下载文件
 * @param fileUrl 文件地址
 * @param customFileName 文件名称
 * @param fileExtension 文件后缀
 * @returns
 */
export const downFetchFile = async (
    fileUrl: string,
    customFileName: string,
    fileExtension: string
) => {
    if (!fileUrl || !customFileName || !fileExtension) {
        throw new Error('缺少必传参数');
        return;
    }

    const type = fileType();

    try {
        const response = await fetch(fileUrl);

        if (!response.ok) {
            throw new Error('Network response was not ok');
        }

        const blob = await response.blob();
        const zipBlob = new Blob([blob], {type: type[fileExtension]});
        saveAs(zipBlob, `${customFileName}.${fileExtension}`);
    } catch (error) {
        console.error('Error downloading file:', error);
    }
};
 类似资料:
  • 我想从GitHub的私有存储库下载,所以我需要传递头和。 例如,使用curl:

  • 问题内容: 如何使用我自己的自定义文件名存储文件? 如果我的自定义文件名需要包含同一项目中的另一个抓取字段,该怎么办?例如,使用和和图像的文件名。如果我理解正确,那将涉及以某种方式从图像管道访问其他项目字段。 任何帮助将不胜感激。 问题答案: 这就是我在Scrapy 0.10中解决问题的方式。检查FSImagesStoreChangeableDirectory的persist_image方法。下载

  • 问题内容: 我是selenium的新手,我想在特定的自定义文件夹中使用selenium chrome Web驱动程序下载文件。默认情况下,文件正在浏览器指定的下载路径中下载。任何人都建议在C#Selenium中以自定义路径下载文件的最佳解决方案。 问题答案: 希望对您有帮助!

  • 10.1.1action通信自定义action文件 action、srv、msg 文件内的可用数据类型一致,且三者实现流程类似: 按照固定格式创建action文件; 编辑配置文件; 编译生成中间文件。 1.定义action文件 首先新建功能包,并导入依赖: roscpp rospy std_msgs actionlib actionlib_msgs; 然后功能包下新建 action 目录,新增 X

  • 我是selenium的新手,我想使用selenium chrome Web驱动程序在特定的自定义文件夹中下载文件。默认情况下,该文件正在浏览器指定的下载路径中下载。任何一个建议在C#Selenium的自定义路径中下载文件的最佳解决方案。

  • Gradle版本:3.5 我试图发布我的自定义每1k展现的收入神器,但留档是真的不清楚这应该如何做。 这是我们发布的gradle脚本的摘录: 我不确定应该如何在发布闭包中引用每1k展现的收入工件的自定义。使用工件名称('our-software-rpm')不起作用,使用任务名称(rpmArts)也不起作用。那么我该怎么办? 此外,该项目还应用java插件来获取一个ArtifactHandler,该

  • 本文向大家介绍magento 自定义日志文件,包括了magento 自定义日志文件的使用技巧和注意事项,需要的朋友参考一下 示例 这将登录到            

  • 我创建了一点js,它将三个变量传递给我的php脚本loadtable.php.这个php文件存储在我的子主题文件夹中,其内容如下: 页面上的js部分: 当页面加载或调用selectChart()时,我只是无法获得do_shortcodePHP脚本来呈现表。你们知道我的解决办法是什么吗?提前感谢。