cancel
Showing results for 
Search instead for 
Did you mean: 

Forcing a download of common mimetypes (xls, csv, txt)

sgomez
Champ in-the-making
Champ in-the-making
Hello,  I've looked all over for an answer to my question and have been unsuccessful thus far.  The wiki pages helped in giving me a better understanding of how to output my template through the browser, but not as an attachment that can be downloaded.  The closest I've come to an answer is from the following thread: http://forums.alfresco.com/en/viewtopic.php?f=36&t=14134&p=46633&hilit, although this topic was for a different file type (xls) which required adding a custom mimetype dfinition.  In my case, I want to give the user the option to download the output as a textfile. 

Is there a way to add a header file to a freemarker template so I can allow the user to download the output result of my template as text or any other common mimetype?  Thanks in advance.

My code is below and it works fine, I just don't know how to add a header to it.

Result: ${total} found
   <#list nodes as document>
      <#if document.isDocument>
                   {"name": "${document.name}"
         <#if document.properties.author??>, "author":"${document.properties.author}"</#if>
         <#if document.properties.creator??>, "creator":"${document.properties.creator}"</#if>
         <#if document.properties.modified??>, "modified":"${document.properties.modified?datetime}"</#if>
         <#if document.properties.created??>, "created":"${document.properties.created?datetime}"</#if>
         <#if document.properties.description??>, "description":"${document.properties.description}"</#if>
      </#if>
</#list>
3 REPLIES 3

sgomez
Champ in-the-making
Champ in-the-making
I came up with a solution that worked out well for me, but did not involve putting to use a header.

I ended up following the post I linked in my previous post and modified my webscript-framework-application-context.xml to add a new  format call "txt," which extended the "text" property and appending "application" to the path(?). <prop key="txt">application/txt+text</prop>  The context file snippet is below:

   <!–  Default Formats –>s
   <bean id="webscripts.formats" parent="webscripts.formatmap">
      <property name="formats">
         <props>
            <prop key="html">text/html</prop>
            <prop key="text">text/plain</prop>
            <prop key="xml">text/xml</prop>
            <prop key="atom">application/atom+xml</prop>
            <prop key="atomentry">application/atom+xml;type=entry</prop>
            <prop key="atomfeed">application/atom+xml;type=feed</prop>
            <prop key="rss">application/rss+xml</prop>
            <prop key="json">application/json</prop>
            <prop key="opensearchdescription">application/opensearchdescription+xml</prop>
            <prop key="mediawiki">text/plain</prop>
            <prop key="portlet">text/html</prop>
            <prop key="fbml">text/html</prop>
            <prop key="php">text/html</prop>
            <prop key="js">text/javascript</prop>
            <prop key="calendar">text/calendar</prop>
         <prop key="xls">application/vnd.ms-excel</prop>
         <prop key="txt">application/txt+text</prop>
         <prop key="csv">application/cv+text</prop>
         </props>
      </property>
      <property name="mimetypes">
         <props>
            <prop key="text/html">html</prop>
            <prop key="text/plain">text</prop>
            <prop key="text/xml">xml</prop>
            <prop key="text/calendar">calendar</prop>
            <prop key="application/atom+xml">atom</prop>
            <prop key="application/atom+xml;type=entry">atomentry</prop>
            <prop key="application/atom+xml;type=feed">atomfeed</prop>
            <prop key="application/rss+xml">rss</prop>
            <prop key="application/json">json</prop>
            <prop key="application/opensearchdescription+xml">opensearchdescription</prop>
         </props>
      </property>
   </bean>

sgomez
Champ in-the-making
Champ in-the-making
I changed my title to be more clear with what I'm trying to achieve (and perhaps what others are trying to find through the forums).

I've come back to this problem as I neglected to test it out in IE7.  I am able to force a download on text file on every browser (FF, Opera, Chrome) except IE.

I tried creating a new mimetype in webscript-framework-application-context.xml called "txt" but IE still reads it as a text file and opens it in a browser.

   <!–  IE Specific set of API Formats –>
   <bean id="webscripts.formats.IE" parent="webscripts.formatmap">
      <property name="agent"><value>MSIE</value></property>
      <property name="formats">
         <props>
         <prop key="text">text/plain</prop>
            <prop key="atom">text/xml</prop>
            <prop key="atomentry">text/xml</prop>
            <prop key="atomfeed">text/xml</prop>
            <prop key="rss">text/xml</prop>
         <prop key="json">application/json</prop>
            <prop key="opensearchdescription">text/xml</prop>
         <prop key="xls">application/vnd.ms-excel</prop>
         <prop key="csv">application/vnd.csv+text</prop>
         <prop key="txt">application/octetstream.txt+text</prop
    [color=#FFBF00] <!– blow are other combinations I found on the web to work in PHP, ASP, etc but not through Alfresco –>[/color]
         <prop key="txt">application/download.txt+text</prop>
         <prop key="txt">application/vnd.txt+text</prop>
         <prop key="txt">application/vnd.txt/text</prop>
         <prop key="txt">application/vnd.txt</prop>

         </props>
      </property>
   </bean>

If anyone found a workaround this, I would greatly appreciate your help.  Thanks!

mdavid_cu
Champ in-the-making
Champ in-the-making
This post have a long time, but recently, I have had the same ISSUE… so, I was able to find a solution, perhaps you find it useful

When we implement a webscript, the JS Controller doesn't have an option to change the response headers, but if we use a Java Backed Controller then we can modify the default response header. In order to achieve a generic manner for forcing the download webscript, I use a webScript service with two controllers:
a) a Java Backed Controller
b) a JS Controller.

The Java Code:

public class ForceDownloadWebScript extends DeclarativeWebScript {
   
   private WebScriptResponse res;

   @Override
   protected void executeFinallyImpl(WebScriptRequest req, Status status,
         Cache cache, Map<String, Object> model) {
      super.executeFinallyImpl(req, status, cache, model);
      String filename = req.getParameter("filename");
      res.addHeader("Content-Type", "application/octet-stream");
      res.addHeader("Content-Disposition", "attachment; filename=" + filename);
   }
   
   @Override
   protected Map<String, Object> createScriptParameters(WebScriptRequest req,
         WebScriptResponse res, ScriptDetails script,
         Map<String, Object> customParams) {      
      this.res = res;
      return super.createScriptParameters(req, res, script, customParams);
      
   }

}

The XML Bean definition:

<bean id="webscript.download.service.get" class="com.my.ForceDownloadWebScript"
          parent="webscript">       
</bean>

Now we implement a service named
alfresco/extension/templates/webscripts/download/service.get.desc.xml

<webscript>
   <shortname>Download Test Service</shortname>
   <description>To Test if this had a force download header</description>
   <url>/force/download/service?filename={filename}</url>
   <authentication>user</authentication>
   <format default="xml">extension</format>
</webscript>

alfresco/extension/templates/webscripts/download/service.get.js

model.animals= [
   {kind: 'dog', name: 'pluto'},
   {kind: 'cat', name: 'tommy'}
];

alfresco/extension/templates/webscripts/download/service.get.xml.ftl

<?xml version="1.0"?>
<animals>
    <#list animals as anim>
       <animal kind="${anim.kind}">${anim.name}</animal>
    </#list>
</animals>


Then you should be able to obtain a downloaded file instead of being opened with browsers
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.