Skip to content

LocalFileStorage

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

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

The LocalFileStorage constructor requires an explicit true or false for allowDuplicates, which has no default value.

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, allowDuplicates: false, ".txt");
byte[] content = Encoding.UTF8.GetBytes("abc");
string publicUrl = storage.Save(content, "note.txt");
Console.WriteLine(publicUrl);

The example returns this URL.

/uploads/ba/78/16bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.txt

The same relative path appears on disk beneath wwwroot/uploads.

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 and the original extension with its case preserved. An extensionless name produces an extensionless file when the storage settings allow it.
  4. When a new file is needed, the provider writes the bytes to a temporary file in the second directory and moves it to the final name once the write completes.

With allowDuplicates: false, if the file already exists at its hash-derived path, Save returns its URL without rewriting it. With allowDuplicates: true, if the name is already in use, Save adds the first available positive integer before the extension, such as -1, -2, or -3. If a numbered file is deleted, its suffix can be used again.

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