0

I'm using Sitecore 9.1.1 and SXA 1.8.1.

I have a case in which i need to make some files downloadable for the user when he clicks on it. I was able to make the files with "pdf and docx" extensions downloadable. However, i can't make .txt files to donwload. When clicking on it, the user is redirected to page previewing the file.

I checked the sitecore.config file to enable "force download" as i did for the png and docx mimetypes but i can't find a mimetype for txt files. i'm not sure if .txt files are not really supproted by Sitecore to be downloadable.

Is there a way to do so?

2 Answers 2

4

There is a mediaType and mimeType defined for txt file in the Sitecore config, in much the same way as it does for pdf and docx, but that is all that it defines:

<mediaLibrary>
  <mediaTypes>
    <mediaType extensions="txt,asc,bas,c,h">
      <mimeType>text/plain</mimeType>
    </mediaType>
  </mediaTypes>
</mediaLibrary>

You can patch the above to add a forceDownload=true element, which will then add the correct headers to force the browser to prompt to save the file(s), rather than opening in the browser tab itself. You can use the following patch:

<configuration>
  <sitecore>

    <mediaLibrary>
      <mediaTypes>
        <mediaType extensions="txt,asc,bas,c,h">
          <forceDownload>true</forceDownload>
        </mediaType>
      </mediaTypes>
    </mediaLibrary>

  </sitecore>
</configuration>

Note that the other extension types will also have the same behaviour applied.

Alternatively, you could add the download attribute to the link to tell the browser to download it (previous question and blog post)

0

The mime type for .txt is text/plain.

Sitecore has nothing to do with this. It's an issue between the correct mimetype, the force download flag, and the browser.

Source: Incomplete list of MIME types

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.