Skip to content

Commit 7e6adcc

Browse files
committed
Blog post data
1 parent 888421c commit 7e6adcc

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<post>
3+
<title>Sql file stream in ASP.NET MVC with Entity framework</title>
4+
<slug>sql-file-stream-in-asp.net-mvc-with-entity-framework</slug>
5+
<author></author>
6+
<pubDate>2015-06-13 18:52:44</pubDate>
7+
<lastModified>2015-06-13 19:57:16</lastModified>
8+
<excerpt></excerpt>
9+
<content>&lt;blockquote&gt; &lt;p&gt;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.&amp;nbsp; &lt;a title="https://msdn.microsoft.com/en-us/library/gg471497.aspx" href="https://msdn.microsoft.com/en-us/library/gg471497.aspx"&gt;https://msdn.microsoft.com/en-us/library/gg471497.aspx&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;To learn more about sql file streams I decided to create simple image gallery.&lt;/p&gt; &lt;p&gt;&lt;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"&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;First of all you need to &lt;a href="https://msdn.microsoft.com/en-us/library/cc645923.aspx"&gt;enable File stream&lt;/a&gt; for sql server instance.&lt;/p&gt; &lt;p&gt;Then create new file stream for database:&lt;/p&gt; &lt;p&gt;&lt;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"&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Specify where to save file stream data by adding new file:&lt;/p&gt; &lt;p&gt;&lt;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"&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;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.&amp;nbsp; Sample table that also has title and primary key:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;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]
22+
GO
23+
&lt;/pre&gt;
24+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
25+
&lt;p&gt;Entity framework model:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;[Table("Files")]
26+
public class FileModel
27+
{
28+
public int Id { get; set; }
29+
public string Title { get; set; }
30+
public byte[] File { get; set; }
31+
}
32+
&lt;/pre&gt;
33+
&lt;p&gt;Note that IdFile column is not present, it has default value and is of no use for us, it is used by sql server.&lt;/p&gt;
34+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
35+
&lt;p&gt;View model used for creating files:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;public class FileViewModel
36+
{
37+
public string Title { get; set; }
38+
public HttpPostedFileBase File { get; set; }
39+
}
40+
&lt;/pre&gt;
41+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
42+
&lt;p&gt;To save files with entity framework we don’t need any specific code, just read all bytes of received file:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;[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+
&lt;/pre&gt;
61+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
62+
&lt;p&gt;To display images we can simply convert bytes to base64 string:&lt;/p&gt;&lt;pre class="brush: csharp;"&gt;&amp;lt;td&amp;gt;
63+
@{
64+
var base64 = Convert.ToBase64String(item.File);
65+
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
66+
}
67+
&amp;lt;img src="@imgSrc" style="max-height:250px"/&amp;gt;
68+
&amp;lt;/td&amp;gt;
69+
&lt;/pre&gt;
70+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
71+
&lt;p&gt;After uploading several files, go to file stream location that you specified.&lt;/p&gt;
72+
&lt;p&gt;&lt;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_5.png" width="579" height="163"&gt;&lt;/p&gt;
73+
&lt;p&gt;You should see one or several folders with guid names, in one of these folders should be files without extensions also with guid names:&lt;/p&gt;
74+
&lt;p&gt;&lt;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_6.png" width="653" height="117"&gt;&lt;/p&gt;
75+
&lt;p&gt;These are uploaded images, you can open it with any image viewer, even paint will do the trick.&lt;/p&gt;
76+
&lt;p&gt;&lt;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_7.png" width="548" height="388"&gt;&lt;/p&gt;
77+
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
78+
&lt;p&gt;&lt;a href="https://github.com/FloatInCodeBlog/SqlFileStreams"&gt;Source code.&lt;/a&gt;&lt;/p&gt;
79+
&lt;p&gt;Database back up included in source (FilesTable.bak).&lt;/p&gt;</content>
80+
<ispublished>true</ispublished>
81+
<categories>
82+
<category>MSSQL</category>
83+
<category>Entity framework</category>
84+
<category>MVC</category>
85+
</categories>
86+
<comments></comments>
87+
</post>
88.3 KB
Loading
44.9 KB
Loading
76.1 KB
Loading
49.1 KB
Loading
23.4 KB
Loading
93.4 KB
Loading

0 commit comments

Comments
 (0)