Search code examples
phpjavascriptjquerysave-as

i want to change the save as dialog filename when the user right clicks


I am using PHP and javascript/jQuery for my project. I want to change the filename for the save as dialog when the user right clicks on an image and selects save as. (for example i want to name every "save dialog" filename "image.png"). Thanks in advance.


Solution

  • The file name that the browser gives in the save dialog CAN be changed, but not in Javascript. Usually this is done when you don't give a direct link to an image, but use a PHP script such as < img src="image.php?id=18" >

    To do this you just need to send proper http headers in image.php, for example:

    header('Content-type: image/jpg');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    

    If you use non-ASCII file name, you will need to encode it and unfortunately the encoding is different for different browsers:

    • For IE use rawurlencode($filename)
    • For FF use base64 with charset specification, such as '=?UTF-8?B?'.base64_encode($filename).'?='

    If you specify "attachment" instead of "inline" in Content-Disposition header, the browser will not try to display the image but will immediately prompt user to download it.