1

I have a page in my site, displaying images. When I right click on an image and click Save Image As I get as default name. I want to change the image name on right click and save it.

This is for example:
In my HTML code I have
image src="abc.jpeg"

When I right click, I want this image to be saved as def.jpg.

Is there a way to do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

4 Answers4

1

As far as I know, you can't do that with JavaScript. This is something that can only be handled on the server-side.

If you're using PHP, a simple solution would be to use header().

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

You can replace download.pdf by a variable of course.

header('Content-Disposition: attachment; filename="' . $fileName .'"');

From the PHP documentation:

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

Liyali
  • 5,643
  • 2
  • 26
  • 40
  • What you've mentioned applies for ** FILE DOWNLOAD ** kinda thing. This is purely a javascript thing. You can edit context menu only by disabling default. – SachinGutte Feb 21 '12 at 18:12
  • thanx can u give me an example ?? what will be the html & php code – Maths Brain Teasers Feb 21 '12 at 18:15
  • @Liyali the question is asked for saving the image file as different name. We can disable context menu, can create our own when clicked on certain elemetn and execCommand can let us open **"save as"** dialog box to save it with different name. RTE's like TinyMCE of wordpress's and other CMS'salso make use of execCommands. Only as execCommand differ in behavior depending upon browsers, care should be taken. – SachinGutte Feb 21 '12 at 18:38
0

Don't do it, not every browser will support it (opt-out for contextmenu events is also possible in some). Why should you want to do so?

I guess you want to link to the big versions of your thumbnails. So just put a link (<a>) around your image tags and everyone will be happy. When you indicate the link with a descriptive tooltip, it would be even better.


EDIT:

When you have an image already included in a page, you can't change the default title the user agent will offer when you click [Save as]. Point, no exeptions.

The browser will likely use the file name from the download path. You can't change the src attribute dynamically (by script), because that would make another file load. What you can do is deliver the page with the files already under their aspired file names, but is to be done at the server.

So option #2 is starting a (new) file download. In the Content-Disposition header you can dynamically (at the server) propose any filename for the sent content. For starting the download, just wrap your image into a link (of which you even can change the href attribute). Or you might want to build a custom context menu to display the download link as a [Save as] button (how to do so would be the matter of a different question).

Option #3 would be to open the file in a new tab/window, apply document.execCommand("SaveAs", [...]) on it and close the tab/window again. Unfortunately, this is only supported in Internet Explorer; see Does execCommand SaveAs work in Firefox? for this and the suggestions.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanx , but i want it to save image with different name than in src. I want this to get implemented in firefox & chrome only..... – Maths Brain Teasers Feb 21 '12 at 18:12
  • You can't set "file names" of downloads with JavaScript. You need to send a specific header (see above @Liyali), which the download dialogue will accept. Context-Menu -> [save as] can't be changed. – Bergi Feb 21 '12 at 18:22
  • @Maths Brain teasers: you can't make it client-side. There is nothing to see with web browsers, it's just a server-side setting. – Liyali Feb 21 '12 at 18:37
0

Well there's a way to do this.. but i'm not sure if it works with most of the browsers. add oncontextmenu="return false;" attribute in your body tag. It will disable the context menu (hopefully ?!) !

And if you want custom context menu then do it with little css and script. Attach event handler for click event and carry out action when left or right click is occurred.

SachinGutte
  • 6,947
  • 5
  • 35
  • 58
  • The question was not on how to change or even disable the contextmenu, but on how to "carry out action". – Bergi Feb 21 '12 at 22:01
-1

You put this in the image

<img src=".." onClick="this.src='new URL'">

or you can do it in a separate function

<img src=".." onClick="change(this)">
//
<script>
function change(img) {
img.src = 'new src';
}
</script>
Eduardo Iglesias
  • 1,056
  • 18
  • 42
  • "When I right click on an image and click Save Image As" not when left click and change to different src. – Aurimas Ličkus Feb 21 '12 at 17:54
  • I dnt think it can work....as this.src='new URL' will change the url as well..... and i just need image name to be changed while saving – Maths Brain Teasers Feb 21 '12 at 18:13
  • @MathsBrainTeasers while no put a button that download & change the image name. Open a new window with header of download header('Content-Disposition: attachment; filename="img.png"'); In many browsers the popup will never appear – Eduardo Iglesias Feb 22 '12 at 02:07