你如何存储asp.net应用程序的图像?

时间:2022-04-26 04:14:14

We are just upgrading our ASP.Net shop implementation (from simple one) to structure that should be more flexible. I am just looking for some options how to store images related to product. (there will be two types of images, thumb of the product and then full view images of the product). It should handle at least hundred products.

我们只是将我们的ASP.Net商店实施(从简单的实施)升级到应该更灵活的结构。我只是在寻找一些如何存储与产品相关的图像的选项。 (将有两种类型的图像,产品的拇指,然后是产品的全视图图像)。它应该处理至少一百种产品。

So far I am thinking about two options:
1) store images in db - images are loaded from Db into stream then into image (displayed by using IHttpHandler)
Pros
- The image itself is part of class, business object which we are working with in code behind
- one place to maintain product data
Cons
- memory consumption - traffic increase as we get the product data from other API

到目前为止,我一直在思考两个选择:1)将图像存储在数据库 - 图像从数据库加载到数据流,然后将图像(通过使用的IHttpHandler)优点显示 - 图像本身是一流的,业务对象,我们都在努力的一部分代码隐藏 - 维护产品数据的一个地方缺点 - 内存消耗 - 当我们从其他API获取产品数据时,流量增加

2) store images in file system - images are put in page as link
Pros
- none impact on memory as it is not stored in session, app cache.It is used like simple link
- no memory consumption
Cons
- needs to keep some name convention for images in File system (perhaps even some folder structure)
- more complicated maintenance of images

2)将图像存储在文件系统中 - 图像作为链接放在页面中优点 - 没有影响内存,因为它没有存储在会话,应用程序缓存中。它像简单的链接一样使用 - 没有内存消耗缺点 - 需要保留一些名称约定对于文件系统中的图像(甚至可能是某些文件夹结构) - 更复杂的图像维护

Is there any other suitable mechanism? What would you advice to use?

还有其他合适的机制吗?你会建议使用什么?

Personally I prefer images in file system, but it can be harder to maintain it as there are two separate places.

我个人更喜欢文件系统中的图像,但由于存在两个不同的位置,因此可能难以维护它。

Thanks for any comment. X.

谢谢你的评论。 X。

BTW: I can really imagine that in some stage the product will also have some videos or any other media that need to be displayed too. In this case Db is not really option, isn't it?

顺便说一句:我真的可以想象,在某个阶段,产品也会有一些视频或任何其他需要显示的媒体。在这种情况下,Db不是真正的选择,不是吗?

5 个解决方案

#1


I made a system similiar to this. In my opinion, page load speed trumps all other considerations so I went with the 'store images on disk' option.

我制作了一个与此类似的系统。在我看来,页面加载速度胜过所有其他考虑因素,所以我选择了“在磁盘上存储图像”选项。

When images are added to the system, the original image is cropped and resized to the desired display size for browsing and a thumbnail is also generated. Three images are then stored to disk, the original, the display size and the thumbnail. Each one has a GUID generated for its filename.

当图像被添加到系统时,原始图像被裁剪并调整大小到所需的显示尺寸以供浏览,并且还生成缩略图。然后将三个图像存储到磁盘,原始图像,显示大小和缩略图。每个人都有为其文件名生成的GUID。

(Imagine shopping on amazon, when you a browsing a list of products only the thumbnail is visible. When you inspect a product its display size is shown, and you can usually click on the image again to see the full size.)

(想象一下在亚马逊上购物,当您浏览产品列表时,只有缩略图可见。当您检查产品时,会显示其显示尺寸,您通常可以再次单击该图像以查看完整尺寸。)

I had a database table that looked a bit like this.

我有一个看起来有点像这样的数据库表。

ID                int, PK
FullSizePath      varchar(128)
DisplaySizePath   varchar(128)
ThumbNailPath     varchar(128)
OriginalData      BLOB

I kept the original data in the db just incase there was an accident on the file server and the images got deleted, so they could all be re-generated. But this data is never pulled from the db during a page request.

我将原始数据保存在数据库中,只是在文件服务器上发生意外并且图像被删除,因此它们都可以重新生成。但是在页面请求期间从不从数据库中提取此数据。

#2


i think mixture of both is the best, for small critical image db is preferable and for large in amount and size file system is better

我认为两者的混合是最好的,因为小的关键图像db是优选的,并且对于大量和大小的文件系统更好

#3


I guess you have covered anything. You can store the main image in filesystem/db but you can generate the thumbnails on the fly programatically, which would reduce some disk space.

我想你已经覆盖了任何东西。您可以将主映像存储在filesystem / db中,但是可以以编程方式动态生成缩略图,这样可以减少一些磁盘空间。

#4


In instances like your example, where I work we tend to store the images on the disk, and then keep a record of the filename in the db.

在您的示例中,我工作的地方倾向于将图像存储在磁盘上,然后在db中保留文件名的记录。

Then when you have a db of your products, you can look up and dynamically load your image per product.

然后,当您拥有产品的数据库时,您可以查找并动态加载每个产品的图像。

This also is quite nice if you have an admin system for adding/removing/editing products.

如果您有一个用于添加/删除/编辑产品的管理系统,这也非常好。

I suppose it's kind of in between your two original suggestions, you can even store all the images in the same directory this way if you like.

我认为它介于两个原始建议之间,如果您愿意,甚至可以将所有图像存储在同一目录中。

#5


If you are using MS SQL 2008, you have an option for the FILESTREAM feature.

如果您使用的是MS SQL 2008,则可以选择FILESTREAM功能。

Here's the gist, but you should read the whole white paper:

这是要点,但你应该阅读整篇白皮书:

FILESTREAM is a new feature in the SQL Server 2008 release. It allows structured data to be stored in the database and associated unstructured (i.e., BLOB) data to be stored directly in the NTFS file system. You can then access the BLOB data through the high-performance Win32® streaming APIs, rather than having to pay the performance penalty of accessing BLOB data through SQL Server.

FILESTREAM是SQL Server 2008发行版中的一项新功能。它允许将结构化数据存储在数据库中,并将相关的非结构化(即BLOB)数据直接存储在NTFS文件系统中。然后,您可以通过高性能Win32®流API访问BLOB数据,而不必支付通过SQL Server访问BLOB数据的性能损失。

FILESTREAM maintains transactional consistency between the structured and unstructured data at all times, even allowing point-in-time recovery of FILESTREAM data using log backups. Consistency is maintained automatically by SQL Server and does not require any custom logic in the application. The FILESTREAM mechanism does this by maintaining the equivalent of a database transaction log, which has many of the same management requirements (described in more details in the “Configuring FILESTREAM Garbage Collection” section later in this white paper). The combination of the database’s transaction log along with the FILESTREAM transaction log allows the FILESTREAM and structured data to be transactionally recovered correctly.

FILESTREAM始终保持结构化和非结构化数据之间的事务一致性,甚至允许使用日志备份对FILESTREAM数据进行时间点恢复。 SQL Server会自动维护一致性,并且不需要应用程序中的任何自定义逻辑。 FILESTREAM机制通过维护数据库事务日志的等效来实现这一点,该日志具有许多相同的管理要求(在本白皮书后面的“配置FILESTREAM垃圾收集”一节中有更详细的描述)。数据库的事务日志与FILESTREAM事务日志的组合允许FILESTREAM和结构化数据在事务上正确恢复。

#1


I made a system similiar to this. In my opinion, page load speed trumps all other considerations so I went with the 'store images on disk' option.

我制作了一个与此类似的系统。在我看来,页面加载速度胜过所有其他考虑因素,所以我选择了“在磁盘上存储图像”选项。

When images are added to the system, the original image is cropped and resized to the desired display size for browsing and a thumbnail is also generated. Three images are then stored to disk, the original, the display size and the thumbnail. Each one has a GUID generated for its filename.

当图像被添加到系统时,原始图像被裁剪并调整大小到所需的显示尺寸以供浏览,并且还生成缩略图。然后将三个图像存储到磁盘,原始图像,显示大小和缩略图。每个人都有为其文件名生成的GUID。

(Imagine shopping on amazon, when you a browsing a list of products only the thumbnail is visible. When you inspect a product its display size is shown, and you can usually click on the image again to see the full size.)

(想象一下在亚马逊上购物,当您浏览产品列表时,只有缩略图可见。当您检查产品时,会显示其显示尺寸,您通常可以再次单击该图像以查看完整尺寸。)

I had a database table that looked a bit like this.

我有一个看起来有点像这样的数据库表。

ID                int, PK
FullSizePath      varchar(128)
DisplaySizePath   varchar(128)
ThumbNailPath     varchar(128)
OriginalData      BLOB

I kept the original data in the db just incase there was an accident on the file server and the images got deleted, so they could all be re-generated. But this data is never pulled from the db during a page request.

我将原始数据保存在数据库中,只是在文件服务器上发生意外并且图像被删除,因此它们都可以重新生成。但是在页面请求期间从不从数据库中提取此数据。

#2


i think mixture of both is the best, for small critical image db is preferable and for large in amount and size file system is better

我认为两者的混合是最好的,因为小的关键图像db是优选的,并且对于大量和大小的文件系统更好

#3


I guess you have covered anything. You can store the main image in filesystem/db but you can generate the thumbnails on the fly programatically, which would reduce some disk space.

我想你已经覆盖了任何东西。您可以将主映像存储在filesystem / db中,但是可以以编程方式动态生成缩略图,这样可以减少一些磁盘空间。

#4


In instances like your example, where I work we tend to store the images on the disk, and then keep a record of the filename in the db.

在您的示例中,我工作的地方倾向于将图像存储在磁盘上,然后在db中保留文件名的记录。

Then when you have a db of your products, you can look up and dynamically load your image per product.

然后,当您拥有产品的数据库时,您可以查找并动态加载每个产品的图像。

This also is quite nice if you have an admin system for adding/removing/editing products.

如果您有一个用于添加/删除/编辑产品的管理系统,这也非常好。

I suppose it's kind of in between your two original suggestions, you can even store all the images in the same directory this way if you like.

我认为它介于两个原始建议之间,如果您愿意,甚至可以将所有图像存储在同一目录中。

#5


If you are using MS SQL 2008, you have an option for the FILESTREAM feature.

如果您使用的是MS SQL 2008,则可以选择FILESTREAM功能。

Here's the gist, but you should read the whole white paper:

这是要点,但你应该阅读整篇白皮书:

FILESTREAM is a new feature in the SQL Server 2008 release. It allows structured data to be stored in the database and associated unstructured (i.e., BLOB) data to be stored directly in the NTFS file system. You can then access the BLOB data through the high-performance Win32® streaming APIs, rather than having to pay the performance penalty of accessing BLOB data through SQL Server.

FILESTREAM是SQL Server 2008发行版中的一项新功能。它允许将结构化数据存储在数据库中,并将相关的非结构化(即BLOB)数据直接存储在NTFS文件系统中。然后,您可以通过高性能Win32®流API访问BLOB数据,而不必支付通过SQL Server访问BLOB数据的性能损失。

FILESTREAM maintains transactional consistency between the structured and unstructured data at all times, even allowing point-in-time recovery of FILESTREAM data using log backups. Consistency is maintained automatically by SQL Server and does not require any custom logic in the application. The FILESTREAM mechanism does this by maintaining the equivalent of a database transaction log, which has many of the same management requirements (described in more details in the “Configuring FILESTREAM Garbage Collection” section later in this white paper). The combination of the database’s transaction log along with the FILESTREAM transaction log allows the FILESTREAM and structured data to be transactionally recovered correctly.

FILESTREAM始终保持结构化和非结构化数据之间的事务一致性,甚至允许使用日志备份对FILESTREAM数据进行时间点恢复。 SQL Server会自动维护一致性,并且不需要应用程序中的任何自定义逻辑。 FILESTREAM机制通过维护数据库事务日志的等效来实现这一点,该日志具有许多相同的管理要求(在本白皮书后面的“配置FILESTREAM垃圾收集”一节中有更详细的描述)。数据库的事务日志与FILESTREAM事务日志的组合允许FILESTREAM和结构化数据在事务上正确恢复。