FILESTREAM enables SQL Server-based applications to store unstructured data, such as documents and images, on the file system. Applications can leverage the rich streaming APIs and performance of the file system and at the same time maintain transactional consistency between the unstructured data and corresponding structured data.

To learn more about sql file streams I decided to create simple image gallery.




First of all you need to enable File stream for sql server instance.

Then create new file stream for database:



Specify where to save file stream data by adding new file:



Table that has file stream column also must have not null, unique rowguidcol column and varbinary column for file content. File content column data will be saved separately and fetched from file during query execution.  Sample table that also has title and primary key:

CREATE TABLE [dbo].[Files](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [IdFile] [uniqueidentifier] unique ROWGUIDCOL  NOT NULL,
    [Title] [nvarchar](max) NULL,
    [File] [varbinary](max) FILESTREAM  NULL,
    [id] ASC


ALTER TABLE [dbo].[Files] ADD  CONSTRAINT [DF_Files_IdFile]  DEFAULT (newid()) FOR [IdFile]


Entity framework model:

public class FileModel
    public int Id { get; set; }
    public string Title { get; set; }
    public byte[] File { get; set; }

Note that IdFile column is not present, it has default value and is of no use for us, it is used by sql server.


View model used for creating files:

public class FileViewModel
    public string Title { get; set; }
    public HttpPostedFileBase File { get; set; }


To save files with entity framework we don’t need any specific code, just read all bytes of received file:

public ActionResult Create([Bind(Include = "Title,File")] FileViewModel fileModel)
    if (ModelState.IsValid)
        var fileData = new MemoryStream();

        var file = new FileModel { Title = fileModel.Title, File = fileData.ToArray() };

        return RedirectToAction("Index");

    return View(fileModel);


To display images we can simply convert bytes to base64 string:

        var base64 = Convert.ToBase64String(item.File);
        var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
    <img src="@imgSrc" style="max-height:250px"/>


After uploading several files, go to file stream location that you specified.


You should see one or several folders with guid names, in one of these folders should be files without extensions also with guid names:


These are uploaded images, you can open it with any image viewer, even paint will do the trick.



Source code.

Database back up included in source (FilesTable.bak).

Check out my other projects
Active forks newsletter
Get a glimpse on open source active forks. Weekly newsletter with selection of repositories and their active forks.


Comments are closed