You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<content><blockquote> <p>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.&nbsp; <a title="https://msdn.microsoft.com/en-us/library/gg471497.aspx" href="https://msdn.microsoft.com/en-us/library/gg471497.aspx">https://msdn.microsoft.com/en-us/library/gg471497.aspx</a></p></blockquote> <p>To learn more about sql file streams I decided to create simple image gallery.</p> <p><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://floatincode.net/posts/files/Sql-file-stream_11E92/image.png" width="470" height="378"></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>First of all you need to <a href="https://msdn.microsoft.com/en-us/library/cc645923.aspx">enable File stream</a> for sql server instance.</p> <p>Then create new file stream for database:</p> <p><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://floatincode.net/posts/files/Sql-file-stream_11E92/image_3.png" width="664" height="366"></p> <p>&nbsp;</p> <p>Specify where to save file stream data by adding new file:</p> <p><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://floatincode.net/posts/files/Sql-file-stream_11E92/image_4.png" width="877" height="249"></p> <p>&nbsp;</p> <p>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.&nbsp; Sample table that also has title and primary key:</p><pre class="brush: csharp;">CREATE TABLE [dbo].[Files](
10
+
[id] [int] IDENTITY(1,1) NOT NULL,
11
+
[IdFile] [uniqueidentifier] unique ROWGUIDCOL NOT NULL,
12
+
[Title] [nvarchar](max) NULL,
13
+
[File] [varbinary](max) FILESTREAM NULL,
14
+
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
15
+
(
16
+
[id] ASC
17
+
))
18
+
19
+
GO
20
+
21
+
ALTER TABLE [dbo].[Files] ADD CONSTRAINT [DF_Files_IdFile] DEFAULT (newid()) FOR [IdFile]
<p>Note that IdFile column is not present, it has default value and is of no use for us, it is used by sql server.</p>
34
+
<p>&nbsp;</p>
35
+
<p>View model used for creating files:</p><pre class="brush: csharp;">public class FileViewModel
36
+
{
37
+
public string Title { get; set; }
38
+
public HttpPostedFileBase File { get; set; }
39
+
}
40
+
</pre>
41
+
<p>&nbsp;</p>
42
+
<p>To save files with entity framework we don’t need any specific code, just read all bytes of received file:</p><pre class="brush: csharp;">[HttpPost]
43
+
[ValidateAntiForgeryToken]
44
+
public ActionResult Create([Bind(Include = "Title,File")] FileViewModel fileModel)
45
+
{
46
+
if (ModelState.IsValid)
47
+
{
48
+
var fileData = new MemoryStream();
49
+
fileModel.File.InputStream.CopyTo(fileData);
50
+
51
+
var file = new FileModel { Title = fileModel.Title, File = fileData.ToArray() };
52
+
db.FileModels.Add(file);
53
+
db.SaveChanges();
54
+
55
+
return RedirectToAction("Index");
56
+
}
57
+
58
+
return View(fileModel);
59
+
}
60
+
</pre>
61
+
<p>&nbsp;</p>
62
+
<p>To display images we can simply convert bytes to base64 string:</p><pre class="brush: csharp;">&lt;td&gt;
63
+
@{
64
+
var base64 = Convert.ToBase64String(item.File);
65
+
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
<p>You should see one or several folders with guid names, in one of these folders should be files without extensions also with guid names:</p>
0 commit comments