You are not logged in Log in Join
You are here: Home » Members » Chui Tey » How to change the name of a downloaded file

Log in
Name

Password

 

How to change the name of a downloaded file

Introduction

Sometimes you may have files which the user can download. However, you might want the default filename that is saved to be different from the id of the file object.

This how to illustrates how to store a file object with id "myFile", but when users download the file the saved file name is NewFileName.exe.

Create a method to change the saved filename

  1. From the management interface, add a "DTML method". When prompted for an id, type "dtmlMyFile".
  2. Click on "Add and Edit"
  3. In the text area, type this (You should enclose Content-Disposition in single quotes - for some strange reason I can't make it appear on the web page)

        <dtml-call "RESPONSE.setHeader( Content-Disposition,Attachment; Filename=NewFileName.exe)">
        

  4. If you prefer Python Scripts:

         hdr_name = "content-disposition"
         hdr_value = attachment; filename="eek.zip" 
         context.REQUEST.RESPONSE.setHeader(hdr_name, hdr_value)
         

Upload your file

  1. From the management interface, add a "File" object. When prompted for an id, type "myFile", and upload a file from your local file system.
  2. In the preconditions field: type
    dtmlMyFile

Test the system

Go to the URL of the file object, eg. http://localhost:8080/myFile and you should be able to download myFile with a different default filename.

Acknowledgements

Thanks to zxc for pointing the header out to me.