Created
September 14, 2022 06:06
-
-
Save YuJianghao/1a7961b51539e948d634a4bf781d7f2b to your computer and use it in GitHub Desktop.
saveAsFileFromUrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FileSaver from "file-saver"; | |
import request from 'umi-request'; | |
export async function downloadFileBlob(url: string) { | |
return request(url, { | |
method: "GET", | |
responseType: "blob", | |
headers: { | |
"Content-Type": "application/octet-stream", | |
}, | |
}); | |
} | |
export async function saveAsFileFromUrl(url: string, fileName?: string) { | |
const blob = await downloadFileBlob(url); | |
await FileSaver.saveAs(blob, fileName ?? url.slice(url.lastIndexOf("/") + 1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment