且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用ASP.NET MVC 5将图像上载到数据库

更新时间:2023-01-03 16:35:55

背景



你需要找到一个答案ASP.NET,MVC只是ASP.NET Web应用程序或网站的子类别。 ASP.NET直接构建,与.NET Framework一起使用的东西也适用于ASP.NET。



查找代码



此项目(问题)已经启动对于许多开发人员来说,使用SQL Server并在其中存储图像。有很多项目或文章。它的基本实现是,





  • 一个处理Web请求的简单ASP.NET代码。
  • SQL Server数据库连接
  • 显示它的网页
if(IsPost) {
   // make sure form has method set to post.
   // get the image
   var image = WebImage.GetImageFromRequest();
}





SQL Server设置



SQL Server,主要使用CE,但Microsoft现在不推荐使用此版本,因此***使用SQL Server Express或任何其他版本(如果需要),以确保连接是建立并且列可以很好地存储数据。通过良好存储我的意思是必须设置每个列的数据类型,在SQL Server中,图像文件(或一般文件)以字节的形式存储,因此SQL Server中的图像数据类型用于保存文件,称为BLOBS或字节等。因此,要将这些文件存储在数据库中,您需要具有Image类型的列。



此外,SQL的此图像数据类型也有限制,如文档链接中所述:





SQL Server setup

SQL Server, mostly CE is used but Microsoft is now deprecating this version, so it is better for you to use the SQL Server Express, or any other version if required, all you need to make sure is that the connection is established and that the columns are good to store the data. By "good to store" I mean that the data type for each of the column must be set, in SQL Server, images file, (or generally files) are stored in the form of bytes, so the Image data type in the SQL Server is for saving the files, as called the BLOBS or bytes etc. So to store these files in the database you need to have a column of type Image.

Also, there is a limit on this Image data type of the SQL too, as stated in the document link:

Quote:

0到2 ^ 31-1(2,147,483,647)字节的可变长度二进制数据。

Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes.

b $ b

http://msdn.microsoft.com /en-us/library/ms187993.aspx [ ^ ]



从SQL Server数据库保存和获取文件



这是您需要被告知如何以及在何处编写代码的任务。 ASP.NET MVP Mike Brind为此撰写了一篇优秀的文章, http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database-with-WebMatrix [ ^ ]



您可以阅读上面的文章,专门了解ASP.NET和SQL Server CE。 SQL Server和SQL Server CE的工作之间没有一般的区别,SQL Server CE中不存在像存储过程那样的一些功能缺失。下面是CodeProject发布的另一篇与此相关的文章,但我自己更喜欢Mike的文章。



C#从数据库保存和加载图像 [ ^ ]



个人意见



通过我个人的发展进步经验,我了解到保存数据库中的文件不是一个好的存储方式文件。您应该始终将文件保存在文件系统中,只需将其链接保存在数据库中。



http://msdn.microsoft.com/en-us/library/ms187993.aspx[^]

Saving and getting files from SQL Server Database

This is the task where you need to be told about how to and where to write the code. ASP.NET MVP Mike Brind has written an excellent article for this, http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database-with-WebMatrix[^]

You can read the above article to learn specifically about ASP.NET and the SQL Server CE. There is no general difference between working of SQL Server and SQL Server CE, just a bit of feature lackages like stored procedures aren't present in the SQL Server CE. Below is another article posted at CodeProject relating to this one, but I myself prefer the Mike's article.

C# Save and Load Image from Database[^]

Personal opinion

Through my personal experience of developmental progress, I have learnt that saving the files inside the Database isn't a good way of storing the files. You should always save the files in the File System, and just save their links in the database.


您可以在此链接中找到答案



http://streetrat.in/street-rat -discussions / save-and-display-Image-using-sql-29.aspx?customauth = 29 [ ^ ]
you can find the answer in this link

http://streetrat.in/street-rat-discussions/save-and-display-Image-using-sql-29.aspx?customauth=29[^]