I have a pdf file of the type byte array. Using Sitecore form custom submit action, I need to download/open the same as pdf in the user's machine. I tried to achieve the same by adding response headers for content disposition but was not successful.

Does anyone have an idea of how to achieve this? Thanks in advance!

enter image description here

I am getting a error in ~\wwwroot\s92.dev.local\Views\FormBuilder\Form.cshtml as

enter image description here

Also in browser I see something like this.. enter image description here

Can someone help?

1 Answer 1


Can you try to do it in the next way:

 ID id = new ID(mediaId);
 if (id.IsNull)
 MediaItem mediaItem = Sitecore.Context.Database.Items.GetItem(id);
 if (mediaItem != null)
                Stream stream = mediaItem.GetMediaStream();
                long fileSize = stream.Length;
                byte[] buffer = new byte[(int)fileSize];
                stream.Read(buffer, 0, (int)stream.Length);
                Response.ContentType = String.Format(mediaItem.MimeType);
                Response.AddHeader("content-disposition", "attachment; filename=" + mediaItem.Name + "." + mediaItem.Extension);

mediaId represent the id of your media item. The code is almost the same with your code. I notice some small differences like: a space before filename when you add header, cache for response and how the stream of pdf is read.

  • Hi..Thanks for your response. I am actually trying to load a file from servie API but not from medialibrary. Hence made changes accordingly but still I see the page as given in the post above.
    – iMathi
    Aug 2, 2021 at 13:08

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.