html5のFileオブジェクトからimg/videoタグで使えるURLを取得する
Nov 4, 2020 15:27 · 108 words · 1 minute read
こんな関数で取得できる
const f = (file: File) => {
const reader = new FileReader();
const promise = new Promise((resolve, reject) => {
reader.onerror = e => reject(e);
reader.onabort = e => reject(e);
reader.onload = e => resolve(e.target?.result);
});
reader.readAsDataURL(file);
return promise;
};
...
const url = f(event.currentTarget.files[0]);
return <video><source src={url} /></video>