4

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

0

Can you try to do it in the next way:

 ID id = new ID(mediaId);
 
 if (id.IsNull)
    return;
 
 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);
                stream.Close();
                Response.Clear();
                Response.ContentType = String.Format(mediaItem.MimeType);
                Response.AddHeader("content-disposition", "attachment; filename=" + mediaItem.Name + "." + mediaItem.Extension);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(buffer);
                Response.End();
            }

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.

1
  • 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 that you have read and understand our privacy policy and code of conduct.

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