我有一个现有的 ASP.NET MVC 2 客户端应用程序,它使用 RESTful WCF 服务应用程序来实现数据持久性。出现了一项新要求,以支持附加/关联到我现有的域对象(产品)之一的图像。
目前,客户端应用程序调用该服务来获取产品列表(以轻量级 ProductInfo 对象列表的形式)并将该列表显示给用户。当用户单击列表中的某个项目时,客户端调用服务来获取支持用户编辑的特定 Product 对象。保存后,客户端将更新的产品发布到服务以实现持久性。
新的要求要求我在列表中显示关联的图像,并允许用户在编辑产品时设置/替换图像。当前图像也会显示在产品编辑器中。每个产品仅关联一张图像,并且该图像是必需的。
-
Stream 是在客户端和服务器之间传递图像数据的最佳方式还是应该使用 Byte[]?
-
对于列表,向 Stream(或 Byte[])类型的 ProductInfo 添加新的 Image 属性是否明智,或者需要单独调用服务来下载图像?
-
同样,对于编辑,我是否只需将图像数据视为任何其他属性,并使用图像属性通过线路来回传递它?
I have an existing ASP.NET MVC 2 client application that consumes a RESTful WCF service application for data persistence. A new requirement has come up to support an image attached/associated to one of my existing domain objects (Product).
Currently, the client application calls the service to obtain a list of Products (in the form of a list of lightweight ProductInfo objects) and displays the list to the user. When the user clicks an item in the list, the client calls the service to get the specific Product object which supports editing by the user. When saved, the client posts the updated Product to the service for persistence.
The new requirement calls for me to display the associated image in the list as well as allow the user to set/replace the image when editing the Product. The current image is also displayed in the Product editor. Only one image will be associated with each Product and the image will be required.
-
Is Stream the best way to pass the image data between client and server or should I go with Byte[]?
-
For the list, would it be wise to add a new Image property to ProductInfo of type Stream (or Byte[]) or require a separate call to the service to download the image?
-
Likewise for editing, do I just treat the image data as any other property and pass it back and forth across the wire using an Image property?
发布评论
评论(2)
这将取决于您使用的绑定,但对于 SOAP,即使您最终选择 Stream,当序列化程序需要通过线路发送它时,它也将是一个 base64 编码的字节数组。
我会单独调用来下载给定产品 ID 的每个图像。这样,您就不必在每次想要查看产品信息而没有预览的情况下下载图像,从而节省带宽。
另一种可能性是从 WCF 服务一次性加载所有图像,然后调用将使用 AJAX 下载它们的控制器操作。然后将它们作为 base64 数据嵌入到 HTML 中(就像 Google 在结果页面中处理图像预览的方式)
为了进行编辑,您可以使用一个服务方法,该方法将获取字节数组和您正在更新的项目的 ID。
This will depend on the binding you are using but with SOAP even if you choose Stream eventually when the serializer needs to send it over the wire it will be a base64 encoded byte array.
I would go with a separate call to download each image given a product id. This way you won't be downloading images every time you want to view product info without previews which could save bandwidth.
Another possibility is to load all the images at one go from the WCF service and then invoke the controller action that will download them using AJAX. Then embed them in the HTML as base64 data (the way Google does with image previews in the results page)
For editing you could have a service method which will take the byte array and the id of the item you are updating.
虽然我很欣赏达林的回应并最初走上了这条路,但我最终采用了专业 ASP.NET MVC 2 框架。
第 6 章描述了如何将图像作为 Product 对象的编辑页面的一部分上传,然后使用控制器操作在另一个页面中显示。我的应用程序中的唯一区别是持久性是通过 RESTful Web 服务接口在不同的层中处理的。然而,根据书中所示的方法,我决定使用单个 DTO,其中包含二进制图像数据和字符串 MIME 类型信息的属性。
如果我有一个不同的 UI 或一个更重的对象要传递,我肯定会重新考虑这种方法,但对我来说,这一次,这非常有效。
While I appreciate Darin's response and originally headed down that path, I ended up going with the same approach outlined in Pro ASP.NET MVC 2 Framework.
Chapter 6 describes how an image can be uploaded as part of the edit page for a Product object then displayed using a controller action in another page. The only difference in my application is that persistence is handled in a different tier via a RESTful web service interface. However, based on the approach shown in the book, I decided to go with a single DTO that contains properties for the binary image data and string MIME type information.
If I had a different UI or a heavier object to pass around, I would definitely reconsider this approach but for me, this time around, this works great.