c# – 以编程方式创建和存储从Web服务检索为byte []数据的图像,作为Umbraco中的媒体项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 以编程方式创建和存储从Web服务检索为byte []数据的图像,作为Umbraco中的媒体项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3286字,纯文字阅读大概需要5分钟。
内容图文
![c# – 以编程方式创建和存储从Web服务检索为byte []数据的图像,作为Umbraco中的媒体项目](/upload/InfoBanner/zyjiaocheng/794/2739c92af3a746c1af523bdb1ad5a5a7.jpg)
我已经能够将一些预定的Web服务调用集成到我的Umbraco站点,该站点使用来自Web服务的响应来更新我站点上的一些内容.我现在可以处理文本和一些其他各种内容,但我的主要问题是我应该如何处理以byte []格式从Web服务提供的图像?
对于一些小环境,我正在开发的网站使用Web服务调用来检索我们的桌面软件用户在其计算机上创建的产品的详细信息.这些产品中的每一个都通过Web服务调用拉入我的Umbraco站点,并在产品的父节点下创建为单独的产品页面.
Products > Product
每个产品都有几个属性,如ID,名称,注释和图像集合.一旦我调用了我的Web服务,我就会使用以下代码创建这些页面:
//Construct Service Request
svc = new StaticDataWebServiceSoapClient();
var response = svc.GetProducts(ref compressionSoapHeader,ref managedUserHeader);
//Umbraco Content Services
int rootID = 1117;
IContentService cs = ApplicationContext.Current.Services.ContentService;
var remove = cs.GetChildren(rootID).Where(x => x.ContentType.Alias == "product");
foreach (var child in remove)
{
cs.Delete(child);
}
foreach(var product in response){
var item = cs.CreateContent(product.ContinentName, rootID, "product");
//Set the properties using the SetValue method, passing in the alias of the property and the value to assign to it
item.SetValue("continentId", product.Id);
item.SetValue("continentName", product.ProductName);
item.SetValue("continentNotes", product.Notes);
foreach (var image in product.Images)
{
??? destination.SetValue("ProductImages", image._Image); ???
image.Caption;
image.FileName;
image.ImageId;
image.KeywordID;
}
cs.SaveAndPublishWithStatus(item);
}
从代码中可以看出,每个产品都有几个与之关联的图像,我也想将其拉入网站并与正在创建的产品页面关联.我该怎么做呢?我是否需要使用媒体服务和特定数据类型,或者这种结构是否适合多媒体选择器?
解决方法:
一旦您检索到图像并为每个图像创建一个新的Media项目,您可能会发现最简单的方法是循环浏览图像,然后使用基于您所记录的多媒体选择器数据类型之类的属性将它们与产品相关联.
由于此数据类型将其值存储为逗号分隔的id列表,因此您可以使用以下内容:
// Setup
IContentService cs = ApplicationContext.Current.Services.ContentService;
var mediaService = ApplicationContext.Current.Services.MediaService;
int mediaRootId = 1111; // Replace with the id of your media parent folder
// Replace the image looping logic with the following:
// MultiMediaPicker stores it's values as a commma separated string of ids.
string imageIds = string.Empty;
foreach (var image in product.Images)
{
var fileName = image.FileName; // Assumes no path information, just the file name
var ext = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
if (!UmbracoConfig.For.UmbracoSettings().Content.DisallowedUploadFiles.Contains(ext))
{
var mediaType = Constants.Conventions.MediaTypes.File;
if (UmbracoConfig.For.UmbracoSettings().Content.ImageFileTypes.Contains(ext))
mediaType = Constants.Conventions.MediaTypes.Image;
var f = mediaService.CreateMedia(fileName, mediaRootId, mediaType);
// Assumes the image._Image is a Stream - you may have to do some extra work here...
f.SetValue(Constants.Conventions.Media.File, fileName, (Stream)image._Image); // Real magic happens here.
mediaService.Save(f);
imageIds += (imageIds.Length > 0 ? "," : "") + f.Id;
}
}
// Get the relevant property on the new item and save the image list to it.
var imagesProp = item.Properties.Where(p => p.Alias == "productImages").FirstOrDefault();
imagesProp.Value = imageIds;
注意 – 我没有测试过这个代码,所以ymmv.
内容总结
以上是互联网集市为您收集整理的c# – 以编程方式创建和存储从Web服务检索为byte []数据的图像,作为Umbraco中的媒体项目全部内容,希望文章能够帮你解决c# – 以编程方式创建和存储从Web服务检索为byte []数据的图像,作为Umbraco中的媒体项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。