1

I tried to download a media item from media library to my local storage using the Item ID. But it downloaded only in cache storage. I want to download it in downloads folder.

public void downloadform(string fileid)
{
    var mediaItem =Database.GetDatabase("master").GetItem(new ID(fileid));
    var media = Sitecore.Resources.Media.MediaManager.GetMedia(mediaItem);

    //Get Media Stream in buffer
    var mediaStream = media.GetStream();
    byte[] buffer = new byte[(int)mediaStream.Length];
    mediaStream.Stream.Read(buffer, 0, (int)mediaStream.Length);

    //Send response to browser
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.ClearContent();
    System.Web.HttpContext.Current.Response.ClearHeaders();
    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    System.Web.HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString());
    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + mediaItem.Name + ".pdf");
    System.Web.HttpContext.Current.Response.BinaryWrite(buffer);
    System.Web.HttpContext.Current.Response.Flush();
    System.Web.HttpContext.Current.Response.End();
}
3

2 Answers 2

0

Use this code the download media into your local folder.

 var mediaItem = Database.GetDatabase("master").GetItem(new ID("{04DAD0FD-DB66-4070-881F-17264CA257E1}"));
    var media = Sitecore.Resources.Media.MediaManager.GetMedia(mediaItem);

    var stream = media.GetStream();

    using (var targetStream = File.OpenWrite(Path.Combine("D:/Go", mediaItem.Name + ".jpeg")))
    {
        stream.CopyTo(targetStream);
        targetStream.Flush();
    }

Modify the code the get the child items and create the loop and set this logic into that.

0

You need to use the Send-File command. When executed on a media library item, it allows you to download the associated blob stored in that item.

Get-Item -Path "master:" -Query "/sitecore/media library/" | Send-File

You can pass -NoDialog to it so the user is not prompted each time.

So for multiple files you can do:

Get-Item -Path "master:" -Query "/sitecore/media library/Images/" | Send-File -NoDialog

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.