Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sergej Roswall Bogachov 1 post 71 karma points
    Apr 08, 2021 @ 11:17
    Sergej Roswall Bogachov
    2

    Safari appending .txt to downloads

    Hello,

    We have recently had an issue with Safari appending a ".txt" extension to downloaded files on our Umbraco site. We managed to find a solution and were encouraged to add it to the forum by Umbraco support, so here it is:

    The problem: Safari is appending ".txt" extension to all downloads of unknown format. In our case, we had files with ".asc" extenstion and the resulting file was renamed to "filename.asc.txt".

    The solution: As it turns out, Safari adds this extension based on the mime type in the Content-Type HTTP header. In our case, the Content-Type for .asc files was always text/plain, causing Safari to interpret the file as a ".txt". The solution was to add the following rewrite rules to the IIS Web.config:

    <rewrite>
          <outboundRules>
            <rule name="Force .asc files mime type" enabled="true">
                <match serverVariable="RESPONSE_Content_Type" pattern=".*" />
                <conditions>
                    <add input="{PATH_INFO}" pattern=".*asc"/>
                </conditions>
                <action type="Rewrite" value="application/octet-stream" />
            </rule>  
            <rule name="Enable content disposition" enabled="true">
                <match serverVariable="RESPONSE_Content_Disposition" pattern=".*" />
                <conditions>
                    <add input="{PATH_INFO}" pattern=".*asc"/>
                </conditions>
                <action type="Rewrite" value="attachment" />
            </rule>
        </outboundRules>
    </rewrite>
    

    The first rule changes the mime type of ".asc" files to "application/octet-stream" which forces Safari to interpret it as a binary file and thus not rename it. The second rule adds a Content-Disposition header which tells browsers that the file should be downloaded rather than viewed in the browser.

    Hope this will help someone in the future!

Please Sign in or register to post replies

Write your reply to:

Draft