iOfficeAI/OfficeCLI · error · ArgumentException
Only base64-encoded data URIs are supported
Error message
Only base64-encoded data URIs are supported
What it means
Thrown by ImageSource.ResolveDataUri when a data URI contains a comma but its header portion (the part before the comma) does not contain 'base64'. OfficeCLI only supports base64-encoded data URIs — URL-encoded (percent-encoded) data URIs are rejected because the decoder expects raw base64. The check is case-insensitive and substring-based against the header.
Source
Thrown at src/officecli/Core/ImageSource.cs:128
if (type == ImagePartType.Jpeg) return "JPEG";
if (type == ImagePartType.Gif) return "GIF";
if (type == ImagePartType.Bmp) return "BMP";
if (type == ImagePartType.Tiff) return "TIFF";
return type.ContentType ?? "unknown";
}
private static (Stream, PartTypeInfo) ResolveDataUri(string dataUri)
{
// Format: data:[<mediatype>][;base64],<data>
var commaIdx = dataUri.IndexOf(',');
if (commaIdx < 0)
throw new ArgumentException("Invalid data URI: missing comma separator");
var header = dataUri[..commaIdx]; // e.g. "data:image/png;base64"
var data = dataUri[(commaIdx + 1)..];
if (!header.Contains("base64", StringComparison.OrdinalIgnoreCase))
throw new ArgumentException("Only base64-encoded data URIs are supported");
// Extract MIME type
var mimeStart = header.IndexOf(':') + 1;
var mimeEnd = header.IndexOf(';');
var mime = mimeEnd > mimeStart ? header[mimeStart..mimeEnd] : header[mimeStart..];
var contentType = MimeToContentType(mime);
var bytes = Convert.FromBase64String(data);
// An empty payload decodes "successfully" to zero bytes and would
// flow all the way into a 0-byte media part — schema-valid, but the
// picture renders broken in Word/PowerPoint. Reject up front, like
// the extpart carrier does for empty data.
if (bytes.Length == 0)
throw new ArgumentException("Invalid data URI: empty base64 payload (would produce a 0-byte image).");
return (new MemoryStream(bytes), contentType);
}
private static (Stream, PartTypeInfo) ResolveUrl(string url)View on GitHub (pinned to 1ced45e900)
Solutions
- Base64-encode the image payload and add ';base64' to the data URI header: 'data:image/png;base64,<base64data>'.
- Use a tool or library function to produce base64 data URIs (e.g. base64 command, or Convert.ToBase64String in C#).
- If you have a URL-encoded data URI, decode it first, then re-encode as base64.
Example fix
// before — URL-encoded data URI rejected add image src='data:image/png,%89PNG%0D%0A...' path='/body' // after — base64-encoded data URI accepted add image src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...' path='/body'
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check that data URI is base64-encoded
if (source.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
int commaIdx = source.IndexOf(',');
if (commaIdx > 0)
{
string header = source[..commaIdx];
if (!header.Contains("base64", StringComparison.OrdinalIgnoreCase))
{
Console.Error.WriteLine("Only base64 data URIs are supported. Re-encode as base64.");
return;
}
}
} Try / catch
try
{
var (stream, contentType) = ImageSource.Resolve(source);
}
catch (ArgumentException ex) when (ex.Message.Contains("base64"))
{
// Re-encode the payload as base64 and retry
} Prevention
- Always use ';base64' in data URIs: 'data:image/png;base64,<data>'.
- Convert URL-encoded data URIs to base64 before embedding.
- Use Convert.ToBase64String to generate the payload in C#.
When it happens
Trigger: Passing a data URI like 'data:image/png,%89PNG...' (URL-encoded, no ;base64 flag) or 'data:text/plain,Hello' (plain text, no ;base64). Any data URI where the header between 'data:' and ',' does not contain the substring 'base64' (case-insensitive) triggers this error.
Common situations: A data URI generated by a browser or tool that defaults to URL-encoding rather than base64. A data URI copied from an HTML source attribute that used percent-encoding. A user constructing a data URI manually without adding the ';base64' parameter.
Related errors
- Invalid data URI: empty base64 payload (would produce a 0-by
- Only base64-encoded data URIs are supported
- Invalid data URI: missing comma separator
- Unsupported MIME type: {mime}. Supported: image/png, image/j
- Invalid data URI: missing comma separator
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/046643a3b9cc08de.
Report an issue: GitHub.