1

Clicking on a link to a PY file in Google Chrome downloads it as desired but doing so in Microsoft Edge and Internet Explorer simply views it.

Is this an issue with the web browsers or the web server? If the latter, how can IIS 10 be configured to always force downloads of PY files?

I have tried the following but nothing has made a difference:

  1. As per https://www.jamf.com/jamf-nation/articles/309/using-iis-to-enable-http-downloads-on-a-windows-server-2008-or-2012-file-share-distribution-point, adding the below MIME types for the extension .py:
    1a. application/octet-stream
    1b. file/download
    1c. application/x-python-code
    1d. text/x-python
  2. As per https://stackoverflow.com/questions/19404770/force-file-download-in-iis7, adding an outbound rewrite rule which resulted in the following values in the file web.config:

 

<outboundRules>
    <rule name="PY_ForceDownload" preCondition="PY_Precondition">
        <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
        <conditions>
            <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.py$" />
        </conditions>
        <action type="Rewrite" value="attachment; filename={C:2}.py" />
    </rule>
    <preConditions>
        <preCondition name="PY_Precondition">
            <add input="{REQUEST_FILENAME}" pattern="\.py$" />
        </preCondition>
    </preConditions>
</outboundRules>
mythofechelon
  • 905
  • 3
  • 24
  • 42

1 Answers1

1

To force a link as a download, it's probably easiest to just modify your html.

<a href="yourPython.py" download>Some link text.</a>

Adding the download attribute will cause the browser to download the file rather than open it (assuming html5).

Here's the w3school's description if you'd like to read more about it.

You could also try setting the MIME Type to file/download in IIS manager.

sippybear
  • 3,197
  • 1
  • 13
  • 12
  • Is this the only way? This particular web site is simply using directory browsing. – mythofechelon Oct 23 '18 at 10:58
  • @mythofechelon Setting the MIME Type for each file extension that you want downloaded to `file/download` should pretty much do it. Did that not help? – sippybear Oct 23 '18 at 15:09
  • No, as I said in the original question, doing that didn't make a difference. – mythofechelon Oct 23 '18 at 15:10
  • @mythofechelon whoops, I missed that. Did you set it at the website level or root IIS? I just built a test site and only set the MIME type for `.py` files at the root level. Download from directory browser works as expected on IE, Edge, Chrome, Opera and Firefox. – sippybear Oct 23 '18 at 16:15
  • I'm fairly sure that I had done it at the site level but I just did so again and it's worked this time. Thanks. – mythofechelon Oct 26 '18 at 14:26