C# 我应该将图像存储在数据库还是文件夹中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/713243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:09:56  来源:igfitidea点击:

Should I store my images in the database or folders?

c#asp.netvb.netimagefile

提问by Etienne

Possible Duplicate:
Storing Images in DB - Yea or Nay?

可能的重复:
在数据库中存储图像 - 是还是不是?

Hi

你好

At the moment each Company on my website have 1 picture they can add to their profile. I save that image into the database....its their Company Logo.

目前,我网站上的每个公司都有 1 张图片可以添加到他们的个人资料中。我将该图像保存到数据库中....它的公司徽标。

Now i want to allow them to add more pictures. Now i don't know if i must save it all in the database or save it in folders????

现在我想让他们添加更多图片。现在我不知道是否必须将其全部保存在数据库中或将其保存在文件夹中????

Reason why i think folders will be better is because there are so much nice articles with fancy silver light kinda features that i can use but all of them only cater for images saved in folders.

我认为文件夹会更好的原因是因为有很多漂亮的文章,我可以使用带有花哨的银色灯光的功能,但所有这些都只适合保存在文件夹中的图像。

And since i am not THAT good its hard for me to change the code to look at the database instead of the examples that uses folders for image retrieval.

而且由于我不是那么好,因此我很难更改代码以查看数据库而不是使用文件夹进行图像检索的示例。

I would like to add something like this to my website (browsing through the images). Any code examples for me on how to do this when images are saved in the database? I am using ASP.NET with VB.net. Click here to view what i am talking about

我想在我的网站上添加类似的内容(浏览图片)。当图像保存在数据库中时,我的任何代码示例如何执行此操作?我在 VB.net 中使用 ASP.NET。 单击此处查看我在说什么

Any ideas guys?

有什么想法吗?

Regards Etienne

问候艾蒂安

采纳答案by Gthompson83

I've done it both ways recently and personally; I do prefer using the directory method for storing the images while keeping their properties in a DB.

我最近和个人都这样做了。我更喜欢使用目录方法来存储图像,同时将它们的属性保存在数据库中。

Main reason: I had client to whom I made a website for. On the Webiste; there was a Photo Gallery section that allowed the user to upload new photos (which could be browsed from the public site). Since my client hasn′t thought on optimizing the images before uploading; the *.jpg was over 1mb. I did implement the ability to update the image (once it was saved to the DB) but it had to be done one record at a time.

主要原因:我有客户,我为其制作了网站。在网站上;有一个照片库部分,允许用户上传新照片(可以从公共网站浏览)。由于我的客户在上传之前没有考虑优化图片;*.jpg 超过 1mb。我确实实现了更新图像的能力(一旦它被保存到数据库中),但它必须一次完成一个记录。

If this happens while storing the images in a directory, then the files can be saved locally, optimized and put back onto the server.

如果在将图像存储在目录中时发生这种情况,则可以将文件保存在本地、优化并放回服务器。

Here is an example

这是一个例子

回答by Program.X

I'd go for folders. More flexibility if you run out of sttorage space (just move them to another disk and re-point), more flexibility with other apps (eg. Silverlight). I'd only use DB for files that had to be secure.

我会去文件夹。如果您的存储空间不足(只需将它们移动到另一个磁盘并重新指向),则灵活性更高,其他应用程序(例如 Silverlight)的灵活性更高。我只会将 DB 用于必须安全的文件。

回答by Steve

The approach I generaly use is to copy images to a folder and keep the relative URLs in the database. The downside to this approach is that if someone deleted the images you end up with "image not found" in your web pages unless you check everytime you render a page.

我通常使用的方法是将图像复制到一个文件夹并将相关 URL 保留在数据库中。这种方法的缺点是,如果有人删除了图像,您最终会在网页中显示“找不到图像”,除非您在每次渲染页面时都进行检查。

回答by MRFerocius

I had the same question for my site too. I've decided to use the folder implementation, because if you use the database, when you want to backup only the pics you shoul backup the whole database... on the other hand you can keep de directory structure, and make an easy backup.

我的网站也有同样的问题。我决定使用文件夹实现,因为如果您使用数据库,当您只想备份图片时,您应该备份整个数据库......另一方面,您可以保留目录结构,并轻松备份.

Separate things my friend, best regards!

把事情分开,我的朋友,最好的问候!

回答by annakata

For any normal site you absolutely want this as part of the site app itself, not stored in a DB. A web site should as much as possible be self contained to keep it portable, and not adding round trips to the DB (even where caching) can only be a good thing. Web servers are verygood at serving image files.

对于任何普通站点,您绝对希望将其作为站点应用程序本身的一部分,而不是存储在数据库中。一个网站应该尽可能地自包含以保持它的可移植性,并且不向数据库添加往返行程(即使在缓存的地方)也只是一件好事。Web 服务器非常擅长提供图像文件。

However, I personally am working on an app where the images are dynamically created and made available to the site through a second management application. Clearly these must be DB backed in some form to keep the images maintainable and secure.

但是,我个人正在开发一个应用程序,其中图像是动态创建的,并通过第二个管理应用程序提供给站点。显然,这些必须以某种形式由数据库支持,以保持图像的可维护性和安全性。

Long story short, where the images have business value (i.e. they're content, they need security, or they're dynamic) you're going to have to store them in a DB. Where they're static and trivial let the website be a website.

长话短说,如果图像具有商业价值(即它们是内容、需要安全性或动态),您将不得不将它们存储在数据库中。在它们是静态和微不足道的地方,让网站成为一个网站。

回答by Dave Van den Eynde

The advantage of using folders is that you don't need to have a custom handler that fetches those BLOBs out of a database and turns them into regular streams. It's also simpler to host them from different locations and you'll avoid the burden on the database server.

使用文件夹的优点是您不需要自定义处理程序来从数据库中提取这些 BLOB 并将它们转换为常规流。从不同位置托管它们也更简单,您将避免数据库服务器的负担。

You have to be careful about storing the pathnames. Relative pathnames work better as URLs and will allow you some flexibility and scaling in where you store them, how you serve them, and so forth.

您必须小心存储路径名。相对路径名作为 URL 工作得更好,并且允许您在存储它们的位置、提供它们的方式等方面具有一定的灵活性和扩展性。

回答by Dave Van den Eynde

I will go for both.

我会去两个。

Firstly, store every image as a BLOB column in database. By doing so, you can be rest assured that the image is always included in your database backup (assuming you do it).

首先,将每个图像存储为数据库中的 BLOB 列。通过这样做,您可以放心,该映像始终包含在您的数据库备份中(假设您这样做了)。

Secondly, copy the file into folder. By doing so, you can avoid from query the database (for the image file) every time. Storing in folder alone may have implications such as

其次,将文件复制到文件夹中。通过这样做,您可以避免每次都查询数据库(用于图像文件)。单独存储在文件夹中可能会产生一些影响,例如

  • file lost due to disk corrupted
  • file are not included in backup operation
  • 由于磁盘损坏文件丢失
  • 文件不包含在备份操作中

So far all of our customer projects are taking the same approach and we have encountered various problems, but no uploaded files are lost. (We are talking about >100GB of uploaded documents).

到目前为止,我们所有的客户项目都采用相同的方法,我们遇到了各种问题,但没有丢失上传的文件。(我们谈论的是 >100GB 的上传文档)。

Note, you may want to look at the following:

请注意,您可能需要查看以下内容:

  1. Ensure that whenever the file is updated / replace, do it on both database and folder. Otherwise, it will be inconsistent.
  2. You may want to have a separate table to store the file's info. Typically you may want to store the following: (1)file name (2)file type (3)file size / mime type
  3. You may want to split the tables that store data. Our practice is once the table reaches 2GB, we create another table. Designed properly, there shouldn't any problem in finding the right table.
  1. 确保无论何时更新/替换文件,都要在数据库和文件夹上进行。否则会不一致。
  2. 您可能需要一个单独的表来存储文件的信息。通常您可能想要存储以下内容: (1) 文件名 (2) 文件类型 (3) 文件大小/ MIME 类型
  3. 您可能希望拆分存储数据的表。我们的做法是一旦表达到 2GB,我们就创建另一个表。设计得当,找到合适的桌子应该没有任何问题。

Hope this helps.

希望这可以帮助。

回答by Jesse C. Slicer

Two thoughts on this:

对此有两个想法:

  1. Use the filesystem. But make sure you design your scheme well enough that any one folder is not overloaded with images and it becomes something of a nightmare to manage. For instance, you can tree it out with first or last letters of the identifier, or subsequent positional notation. Your mileage will vary, so make sure the scheme fits your data set (images) size. No need to go to detailed lengths if you're only managing, say, 20 images.

  2. Use SQL Server 2008. It has a new datatype called filestream while will save the images to the filesystem but allow you to retrieve it via standard database queries. See http://msdn.microsoft.com/en-us/library/cc949109.aspxfor more information.

  1. 使用文件系统。但是请确保您将方案设计得足够好,以免任何一个文件夹中的图像超载,并且管理起来就像一场噩梦。例如,您可以使用标识符的第一个或最后一个字母或随后的位置符号将其树状化。您的里程会有所不同,因此请确保该方案适合您的数据集(图像)大小。如果您只管理例如 20 张图像,则无需详细说明。

  2. 使用 SQL Server 2008。它有一个称为文件流的新数据类型,同时将图像保存到文件系统,但允许您通过标准数据库查询检索它。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/cc949109.aspx

These aren't exclusive choices, but at the very least, I'd recommend option #1 for the sheer fact that you'll get better performance out of using a filesystem-based-scheme rather than storing and reading blobs out of a database.

这些不是唯一的选择,但至少,我推荐选项 #1,因为使用基于文件系统的方案可以获得更好的性能,而不是从数据库中存储和读取 blob .

回答by Mehdi

I think saving images in system folders is the best way.
working with DB and Queries you know will cause some overloads and DB transanctions usually are heavy and it can place more stress on your server.
with the way u save images in folders and just put URLs in DB it is more suitable for your app loads.
but the advantage of DB is that you can BackUp your images.

我认为将图像保存在系统文件夹中是最好的方法。
您知道使用 DB 和查询会导致一些过载,并且 DB 事务通常很重,它会给您的服务器带来更多压力。
通过将图像保存在文件夹中并将 URL 放在数据库中的方式,它更适合您的应用程序加载。
但 DB 的优点是您可以备份您的图像。



My Suggestion: save your Images in folders. the only need is to know about system I/O operations.

我的建议:将图像保存在文件夹中。唯一需要的是了解系统 I/O 操作。

回答by mhd

AFAIK, flickr.com goes with folders/file server as well with the reference,metadata,etc stored in db. See flickr architecture

AFAIK,flickr.com 与文件夹/文件服务器以及存储在 db 中的参考、元数据等一起使用。查看flickr 架构