Skip to content
May 28, 2011 / kiranpatils

Render file in browser rather than showing OpenSaveDialog

Challenge:

One of my friend asked me that he is requesting XML file which has been uploaded in Sitecore using javascript for fetching XML content. But it fails as when he hits that XML URL directly in browser it shows OpenSave Dialog box of browser. Which he wanted to disable.

Solution:

Basically, This is being manged using Web.Config’s MediaType configuration here  <forceDownload>true tag plays a vital role in deciding whether to render or show open save dialog for request media item.If forceDownload is true then it will show OpenSave dialog else it won’t! — Easy right?

Sitecore says :

A true value for the forceDownload element causes Sitecore to apply an HTTP “Content-
Disposition = attachment; filename=” header when linking to the .ashx URL of the media item,
causing the browser to prompt the user to open/save as rather than opening the resource in the
browser.

So, Open you web.config file and find mediaType for your desired extension on which you would like to disable ForceDownload [In this case it’s XML] and set forceDownload to false.

<mediaType name="XML document" extensions="xml">
<mimeType>text/xml</mimeType>
<forceDownload>false</forceDownload>
system/media/unversioned/xml
system/media/versioned/xml
<thumbnails>
<generator type="Sitecore.Resources.Media.MediaThumbnailGenerator, Sitecore.Kernel">
<extension>png</extension>
</generator>
/sitecore/shell/Themes/standard/images/xml.png
</thumbnails>
</mediaType>

Just a note : Browser should also have proper plugin to render the requested media file. E.g. If you set forceDownload false for PDF files than PDF plugin should be there in your browser.

Worth to read : http://sdn.sitecore.net/upload/sdn/articles/media/sitecore-media-facilities.pdf#search=%22Forcedownload%22

Happy Media serving! 🙂

Advertisements

2 Comments

Leave a Comment
  1. Shafaqat Ali / Jul 31 2014 7:22 am

    thanks for this, is it possible to set force download=true for some files and force download=false for some other files?

    • kiranpatils / Aug 3 2014 10:09 pm

      Hello Shafaqat,

      You are welcome! Sorry, haven’t understood your question. But as per my understanding you would like to do force download = true for particular PDF Files and false to other files — is it so? If so, then what will be your logic? Curious to know the reason. The way is : In custom media handler.

      If you would like to do it based on extension then it’s super easy — just add particular extension mediaType

      Thanks,
      Kiran

      Kiran

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: