Last active
March 2, 2017 20:59
-
-
Save pmedcraft/7a061298ae92aad0047f75b26302fee4 to your computer and use it in GitHub Desktop.
C# method for downloading a multimedia asset from SDL Web (Tridion)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Dictionary<string, string> DownloadFromTridion(StreamDownloadClient streamDownloadClient, ComponentData multimediaComponent) | |
{ | |
Dictionary<string, string> downloadInfo = null; | |
// Prepare file for download in local file system | |
string originalFileName = Path.GetFileName(multimediaComponent.BinaryContent.Filename); | |
string downloadFile = Options.MigrationSupportDirectory + originalFileName; | |
FileStream fs = File.Create(downloadFile); | |
try | |
{ | |
// Read metadata fields from Tridion | |
XDocument metadata = XDocument.Parse(multimediaComponent.Metadata); | |
var nss = metadata.Root.GetDefaultNamespace(); | |
string altText = metadata.Root.Element(nss + "AltText") != null ? metadata.Root.Element(nss + "AltText").Value : String.Empty; | |
// Download asset from Tridion | |
byte[] binaryContent = null; | |
if (multimediaComponent.BinaryContent.FileSize != -1) | |
{ | |
Stream tempStream = streamDownloadClient.DownloadBinaryContent(multimediaComponent.Id); | |
var memoryStream = new MemoryStream(); | |
tempStream.CopyTo(memoryStream); | |
binaryContent = memoryStream.ToArray(); | |
tempStream.Close(); | |
} | |
if (binaryContent != null) | |
fs.Write(binaryContent, 0, binaryContent.Length); | |
downloadInfo = new Dictionary<string, string>(); | |
downloadInfo.Add("file", downloadFile); | |
downloadInfo.Add("originalFileName", originalFileName); | |
downloadInfo.Add("altText", altText); | |
downloadInfo.Add("title", multimediaComponent.Title); | |
} | |
catch(Exception exception) | |
{ | |
log.Error(exception); | |
} | |
finally | |
{ | |
fs.Close(); | |
} | |
return downloadInfo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment