| 12345678910111213141516171819202122232425262728 |
- export const urlToBlob = (url: string): Promise<Blob> => {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.open("GET", url, true);
- xhr.responseType = "blob";
- xhr.onload = function () {
- if (this.status === 200) {
- resolve(this.response as Blob); // 显式断言为 Blob 类型
- } else {
- reject(new Error("Error loading image"));
- }
- };
- xhr.onerror = function () {
- reject(new Error("Network error"));
- };
- xhr.send();
- });
- };
- export const urlToFile = async (url: string, fileName: string) => {
- console.log("urlToFile")
- return urlToBlob(url).then(blob => {
- return new File([blob], fileName, {
- type: blob.type,
- lastModified: new Date().getTime()
- });
- });
- }
|