-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ts
34 lines (31 loc) · 921 Bytes
/
Utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Source: https://stackoverflow.com/questions/5933565/how-to-create-initialize-the-file-object-using-file-path-html5
export const getFileBlob = function (url, cb) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
cb(xhr.response);
});
xhr.send();
};
export const blobToFile = function (blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
};
export const getFileObject = function (filePathOrUrl, cb) {
getFileBlob(filePathOrUrl, function (blob) {
cb(blobToFile(blob, "test.jpg"));
});
};
export const getFileObjectAsync = async function (filePathOrUrl: string) {
return new Promise((resolve, reject) => {
try {
getFileBlob(filePathOrUrl, function (blob) {
resolve(blobToFile(blob, "test.jpg"));
});
} catch (error) {
reject(error);
}
});
};