6. File
<? register input @File ?>
<input type="file" name="<?attr OutputId(@File) ?>"><? register input @File base64 = true ?>
<input type="text" id="<?attr OutputId(@File) ?>" name="<?attr OutputId(@File) ?>">
<button id="<?attr OutputId(@File) ?>-btn">Create binary data</button>
<? startupscript ?>
function clickHandler() {
const helloFile = createHelloFile("hello.txt");
// Assign the text field with the formatted contents of `helloFile`.
fileInputEl.value = helloFile;
}
function createHelloFile(fileName) {
if (fileName.includes(";"))
throw new Error("Filename cannot contain a ';'.");
// Create Base64 representation of the text "Hello".
const encoded = btoa("Hello");
// Return the emulated file in the format "filename;base64 data".
return `${fileName};${encoded}`;
}
const fileInputEl = document.getElementById(<?js OutputId(@File) ?>);
const btnEl = document.getElementById(<?js OutputId(@File) ?> + "-btn");
btnEl.addEventListener("click", () => clickHandler());
<? end ?>Last updated
Was this helpful?