Skip to content

Storage LocalFileStorage

github-actions[bot] edited this page Dec 4, 2026 · 1 revision

LocalFileStorage

LocalFileStorage, in the Storage.Files namespace, saves file bytes under RootPath and returns a URL under PublicBaseURL. Configure your web server to serve that directory at the corresponding URL.

Save a file

This example stores a three-byte text file under wwwroot/uploads and prints its URL. The storage accepts files with a .txt extension and a size below 1024 bytes.

using System;
using System.Text;
using Storage.Files;

FileStorage storage = new LocalFileStorage("wwwroot/uploads", "/uploads/", 1024, ".txt");
byte[] content = Encoding.UTF8.GetBytes("abc");
string publicUrl = storage.Save(content, "note.txt");
Console.WriteLine(publicUrl);

At Unix timestamp 1767225600000, the example would return the following URL.

/uploads/ba/78/16bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad-1767225600000.txt

The same relative path appears on disk beneath wwwroot/uploads. The timestamp changes with the time of the save.

How the file name is generated

  1. Save validates the bytes and the original file's extension, then computes the lowercase hexadecimal SHA-256 hash of the content.
  2. The first two pairs of hash digits name two nested directories. For the bytes above, these are ba and 78. The provider creates missing directories.
  3. The file name consists of the remaining 60 hash digits, a hyphen, the UTC Unix timestamp in milliseconds, and the original extension with its case preserved. An extensionless name produces an extensionless file when the storage settings allow it.
  4. The provider writes the bytes to a temporary file in the second directory and moves it to the final name once the write completes. Saving identical bytes with the same extension in the same millisecond produces the same URL and replaces the file at that path.

Delete a file

Pass the exact URL returned by Save to Delete. The provider resolves it beneath RootPath and removes the file. If the file is already absent, the call succeeds. An invalid URL raises ArgumentException.

Related topics

FileStorage and validation · Class source · User manual

Clone this wiki locally