批量下载【上传到OSS的文件】

 arr:由文件地址组成的数组

export async function BatchDownload(arr: any) {
    // 模拟从OSS获取的文件链接
    for (const url of arr) {
        // 使用Fetch API获取文件内容
        const response: any = await fetch(url);
        const fileData: any = await response.blob();

        // 创建Blob对象
        const blob = new Blob([fileData], { type: response.headers.get("content-type") });

        // 创建URL
        const fileUrl = URL.createObjectURL(blob);

        // 创建a标签
        const link = document.createElement("a");

        // 设置a标签的属性
        link.href = fileUrl;
        link.download = url.substring(url.lastIndexOf('/') + 1); // 使用文件名作为下载文件名

        // 将a标签添加到DOM中
        document.body.appendChild(link);

        // 触发下载
        link.click();

        // 移除a标签
        document.body.removeChild(link);

        // 释放Blob对象的URL
        URL.revokeObjectURL(fileUrl);
    }
}

你可能感兴趣的:(javascript,前端,开发语言)