ObjectUrls
Turn any C# byte array into a blob: URL the browser can link to, render or download - a managed wrapper over URL.createObjectURL and URL.revokeObjectURL with automatic cleanup.
@inject Bit.Butil.ObjectUrls objectUrlsMDN reference
Create builds a Blob from your bytes (with the MIME type you pass) and returns its blob: URL. The URL is origin-scoped and works anywhere a regular URL does: anchors, images, iframes, downloads. Type some text, create a URL for it, then open or download the result.
byte[] bytes = Encoding.UTF8.GetBytes("Hello from Bit.Butil!");
string url = await objectUrls.Create(bytes, "text/plain");
// <a href="@url" target="_blank">Open</a>
// <a href="@url" download="note.txt">Download note.txt</a>Any MIME type works. Here an SVG document is composed as a plain C# string, turned into an object URL with the image/svg+xml type and rendered by a regular img tag - no server, no data URL inflation.
var svg = $"<svg xmlns='http://www.w3.org/2000/svg' width='240' height='120'>" +
$"<rect width='240' height='120' rx='12' fill='#4f46e5'/>" +
$"<text x='120' y='68' text-anchor='middle' fill='#fff' font-size='18'>Made at {DateTime.Now:HH:mm:ss}</text>" +
"</svg>";
string url = await objectUrls.Create(Encoding.UTF8.GetBytes(svg), "image/svg+xml");Each object URL pins its Blob in memory until the document unloads or the URL is revoked. Revoke as soon as a URL is no longer referenced - after the download started or the image loaded. Revoking invalidates the URL immediately: links created above stop working.
await objectUrls.Revoke(url);ObjectUrls tracks every URL it creates and revokes any still outstanding when the service is disposed (end of circuit or app scope), so forgotten URLs don't leak. Pass track: false when a URL must outlive the service - then revoking it becomes your responsibility.
// tracked (default): auto-revoked on DisposeAsync if you forget
string tracked = await objectUrls.Create(bytes, "application/pdf");
// untracked: survives disposal; call Revoke yourself
string longLived = await objectUrls.Create(bytes, "application/pdf", track: false);API reference
ValueTask<string> Create(byte[] data, string mimeType = "application/octet-stream", bool track = true)ValueTask Revoke(string objectUrl)ValueTask DisposeAsync()