|
@@ -22,4 +22,38 @@ export const regexUrl = new RegExp(
|
|
|
'i'
|
|
|
);
|
|
|
|
|
|
+export function urlToBlob(url: string, cb: any) {
|
|
|
+ const xhr = new XMLHttpRequest();
|
|
|
+ xhr.open('GET', url, true);
|
|
|
+ xhr.responseType = 'blob';
|
|
|
+ xhr.onload = function () {
|
|
|
+ if (xhr.status === 200) {
|
|
|
+ cb(URL.createObjectURL(xhr.response));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ xhr.send();
|
|
|
+}
|
|
|
+
|
|
|
+export function saveAs(blob: Blob, filename: string) {
|
|
|
+ if ((window as any).navigator.msSaveOrOpenBlob) {
|
|
|
+ (navigator as any).msSaveBlob(blob, filename);
|
|
|
+ } else {
|
|
|
+ const link: any = document.createElement('a');
|
|
|
+ const body = document.querySelector('body') as HTMLBodyElement;
|
|
|
+ link.href = blob;
|
|
|
+ link.download = filename;
|
|
|
+ link.style.display = 'none';
|
|
|
+ body.appendChild(link);
|
|
|
+ link.click();
|
|
|
+ body.removeChild(link);
|
|
|
+ window.URL.revokeObjectURL(link.href);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function downloadByCrossUrl(url: string, filename: string) {
|
|
|
+ urlToBlob(url, (blob: Blob) => {
|
|
|
+ saveAs(blob, filename);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
export default null;
|