简体   繁体   中英

IIS Rewrite Content-Disposition attachment filename

I'm not the greatest with rewrite rules but I'm trying to rename a file a download based on a querystring eg something like this https://example.com/images/02ec1c94de50.jpg?download=1&filename=renamethefiletothis.jpg

this is the rule I have, currently hardcoding the file to be named thisfile.jpg i want to take the URL parameter filename and replace thisfile.jpg

<rule name="Downloadthefile">
    <match serverVariable="RESPONSE_Content_Disposition" pattern=".*" />
    <action type="Rewrite" value="attachment;filename=thisfile.jpg" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="(^|&amp;)download=1(&amp;|$)"/>
    </conditions>
</rule>

Thank you

I was able to get something working this isn't perfect but works.

so this url the file stored on the server is called 02ec1c94de50.jpg but when the user clicks on the link it downloads as myrenamed.jpg

https://example.com/02ec1c94de50.jpg?&filename=myrenamed.jpg

the download attribute eg download="myrenamed" doesn't work crossdomain so have to use the content-disposition header to rename dynamically.

 <rule name="Downloadthefile"> <match serverVariable="RESPONSE_Content_Disposition" pattern=".*" /> <action type="Rewrite" value="attachment;{C:0}" /> <conditions> <add input="{QUERY_STRING}" pattern="(^|&amp;)filename=(.*)$" /> </conditions> </rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM