Andi AR
Andi AR

Reputation: 2918

create sitecore item as pdf

Hi i am doing kind of migrating other cms to sitecore now.

So my requirement is to viewing pdf in the respective url..

Existing site url : DOMAIN.COM/pressrelease/one

And this will show PDF content in a browser.

New site expected Url: NEWDOMAIN.COM/pressrelease/one enter image description here

Similarly on my sitecore content i try to create under root one pressrelease item and its child is one.pdf. But i cant able to view my pdf after this when i gave url like NEWDOMAIN.COM/pressrelease/one.

And need expected behaviour is to open a pdf file in a browser as like media (/-/media/pressrelease/one)items are can able to view.

Upvotes: 0

Views: 575

Answers (2)

Andi AR
Andi AR

Reputation: 2918

I have solved my case using below solution

  1. First i have created one content item NEWDOMAIN.COM/pressrelease/one.

  2. This item having two fields from my template, like shown below.enter image description here

  3. Then i have some separate Layout file for this content item, In the Layout i have created the code for displaying pdf.(Also in some Layouts i can able to download file too using this approach.)

    if(mediaResult.mediaItem != null)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = mediaResult.mediaItem.MimeType;
        HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("inline;filename=\"{0}\"", mediaResult.mediaName));
        HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
        HttpContext.Current.Response.BufferOutput = true;
        // Copy the media stream to the response output stream
        mediaResult.mediaItem.GetMediaStream().CopyTo(HttpContext.Current.Response.OutputStream);
        // As momma always said: Always remember to flush
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
    

Upvotes: 0

Rama Krshna Ila
Rama Krshna Ila

Reputation: 496

Create AllowedExtensions.config in App_config/include folder(if it does not exist already) and add the following sitecore configuration.The configuration makes sure that pdfs are allowed via URL.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <preprocessRequest>
        <processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
          <param desc="Allowed extensions (comma separated)">aspx, ashx, asmx, xml, txt,pdf,png</param>
          <param desc="Blocked extensions (comma separated)">*</param>
          <param desc="Blocked extensions that stream files (comma separated)">*</param>
          <param desc="Blocked extensions that do not stream files (comma separated)"/>
        </processor>
      </preprocessRequest>
    </pipelines>
  </sitecore>
</configuration>

Upvotes: 1

Related Questions