如何使用ASP.NET MVC 5将图像上载到数据库 [英] How can I upload an image to a database using ASP.NET MVC 5

查看:97
本文介绍了如何使用ASP.NET MVC 5将图像上载到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,之前我确实问过这个问题,但是那一刻我试图将图像的路径保存到数据库,复制该图像,然后在我的视图中显示,但现在,我只是搜索上传图像到数据库的方式,使用SQL Server和ASP.NET MVC 5,抱歉重复几乎相同的问题,但他们是不同的,请,有人帮助我,我有相同的问题,差不多一个星期。

Hello everyone, I did ask this question previously, but in that moment I was trying to save the route of an image to the database, copy that image, and then show it in my view, but now, i'm just searching the way to upload an image to the database, using SQL Server and ASP.NET MVC 5, sorry for repeat almost the same question, but they are different, please, somebody help me, I have the same problem for almost a week.

推荐答案

背景



你需要找到一个答案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.

< br $> 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#从数据库保存和加载图像 [ ^ ]



< b>个人意见



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



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.


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



< a href =http://streetrat.in/street-rat-discussions/save-and-display-Image-using-sql-29.aspx?customauth=29> 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[^]


这篇关于如何使用ASP.NET MVC 5将图像上载到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆