文件转 Base64 data URL 在线工具
const input = document.querySelector("#uploadFile");
if (!(input instanceof HTMLInputElement)) throw new TypeError();
const outputWrapper = document.querySelector("#output");
if (!outputWrapper) throw new TypeError();
const fileReader = new FileReader();
/**
* @param {FileList} files
*/
async function handleInput(files) {
for (const file of files) {
await new Promise(resolve => {
fileReader.readAsDataURL(file);
fileReader.addEventListener("load", resolve, { once: true });
});
const details = document.createElement("details");
details.innerHTML = `<summary>${file.name}<button>Copy</button></summary><code>${fileReader.result}</code>`;
const copyButton = details.querySelector("button");
if (!copyButton) throw new TypeError();
copyButton.addEventListener("click", async () => {
await navigator.clipboard.writeText(details.querySelector("code").textContent);
copyButton.innerText = "Copied";
});
outputWrapper.appendChild(details);
}
}
input.addEventListener("change", async () => {
if (!input.files) throw new TypeError();
handleInput(input.files);
});
document.querySelector("#pasteArea").addEventListener("paste", (event) => {
if (!(event instanceof ClipboardEvent)) throw new TypeError();
handleInput(event.clipboardData.files);
});
除非另有标注,内容全部手写或已人工检查所有LLM生成信息无误。
本文来自博客园,作者:145a,转载请注明原文链接:https://www.cnblogs.com/54145a/p/19495744
浙公网安备 33010602011771号