Skip to content

Instantly share code, notes, and snippets.

@YuJianghao
Created September 14, 2022 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuJianghao/1a7961b51539e948d634a4bf781d7f2b to your computer and use it in GitHub Desktop.
Save YuJianghao/1a7961b51539e948d634a4bf781d7f2b to your computer and use it in GitHub Desktop.
saveAsFileFromUrl
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