Skip to content

Instantly share code, notes, and snippets.

@pmedcraft
Last active March 2, 2017 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmedcraft/7a061298ae92aad0047f75b26302fee4 to your computer and use it in GitHub Desktop.
Save pmedcraft/7a061298ae92aad0047f75b26302fee4 to your computer and use it in GitHub Desktop.
C# method for downloading a multimedia asset from SDL Web (Tridion)
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