要在媒体库中下载并保存图像,我使用以下代码。我可以在媒体库中看到一个项目,但它没有媒体。
using (WebClient webClient = new WebClient())
{
byte[] data = webClient.DownloadData("https://myurl/images?id="+12345);
Stream memoryStream = new MemoryStream(data);
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
FileBased = false,
OverwriteExisting = true,
Versioned = true,
IncludeExtensionInItemName = true,
Destination = Factory.GetDatabase("master").GetItem(Settings.GetSetting("ProfilePicturesFolderItemId")).Paths.Path + "/" + "12345",
Database = Factory.GetDatabase("master"),
AlternateText = userProfileItem.Name
};
using (new SecurityDisabler())
{
var creator = new Sitecore.Resources.Media.MediaCreator();
creator.CreateFromStream(memoryStream, v1ImageId, options);
}
}
在Media文件夹中,我可以看到一个名称为" 12345"的项目。但没有媒体。
答案 0 :(得分:1)
从我记忆中,当你传递Destination
选项时,它必须包含项目的新名称,它应该与CreateFromStream
方法调用中的第二个参数相同扩展:
using (WebClient webClient = new WebClient())
{
string filename = "SOMEFILENAME";
string extension = ".png"; // or whatever is the extension
byte[] data = webClient.DownloadData(imageUrl);
Stream memoryStream = new MemoryStream(data);
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
FileBased = false,
OverwriteExisting = true,
Versioned = true,
IncludeExtensionInItemName = true,
Destination = Factory.GetDatabase("master").GetItem(Settings.GetSetting("ProfilePicturesFolderItemId")).Paths.Path + "/" + filename,
Database = Factory.GetDatabase("master"),
AlternateText = userProfileItem.Name
};
using (new SecurityDisabler())
{
var creator = new Sitecore.Resources.Media.MediaCreator();
creator.CreateFromStream(memoryStream, filename + extension, options);
}
}