writeFileAsBase64
Category: File Operations
Namespace: web.files.writeFileAsBase64
Description
Decodes a Base64 string and writes the raw bytes to a file on the local disk. Works with any file type — images, PDFs, audio, documents, or any other binary format. Parent directories are created automatically if they do not exist.
Use this API whenever you need to write binary data to a file. For plain text files, use writeTextFile() instead.
Syntax
const success = await web.files.writeFileAsBase64({
filePath: string,
data: string
});
Parameters
- filePath (required) - Path where the file should be written (relative or absolute)
- data (required) - Base64-encoded binary content to write
Returns
Promise<boolean> - Returns true if the file was written successfully, false otherwise. Throws on error (invalid Base64, access denied, etc.)
Examples
Save a Base64 image to disk
// base64 could come from readFileAsBase64(), fetchToBase64(), a canvas export, etc.
const success = await web.files.writeFileAsBase64({
filePath: 'C:\\Users\\Alice\\Pictures\\copy.jpg',
data: base64String
});
if (success) {
console.log('Image saved successfully');
}
Export a canvas drawing as a PNG
const canvas = document.getElementById('myCanvas');
// toDataURL returns "data:image/png;base64,iVBORw0K..." — strip the prefix first
const dataUrl = canvas.toDataURL('image/png');
const base64 = dataUrl.split(',')[1];
const success = await web.files.writeFileAsBase64({
filePath: 'C:\\Exports\\drawing.png',
data: base64
});
Round-trip: read, process, write back
// Read the original
const base64 = await web.files.readFileAsBase64({
filePath: 'C:\\Photos\\original.jpg'
});
// ... process the data in memory ...
// Write the result to a new path (parent folder created automatically)
const success = await web.files.writeFileAsBase64({
filePath: 'C:\\Photos\\processed\\result.jpg',
data: base64
});
if (!success) {
alert('Failed to save the processed file.');
}
Use Cases
- Save images received as Base64 (from canvas, API responses, etc.) to disk
- Write any binary file to disk from an in-memory Base64 string
- Round-trip binary files with
readFileAsBase64() - Export generated content (canvas drawings, processed images) as files
When to Use
Use writeFileAsBase64() when:
- You have binary data as a Base64 string that you want to save to disk
- You are writing images, PDFs, audio, or any other binary format
Use writeTextFile() when:
- The content is plain text (UTF-8), such as
.txt,.json,.csv,.md
Use download() or downloadFromUrl() when:
- You want to save a remote file directly to disk without loading it into memory first
Notes
- Parent directories are created automatically (no need to create them first)
- Overwrites existing files without warning — check first with
fileExists()if needed - Relative paths are resolved from the application executable directory
- The
dataparameter must be a valid Base64 string — invalid input will throw an error - Base64 encoding is approximately 33% larger than the original binary; the written file will be the correct original size