Question: I'm a beginner with JS and HTML.
I'm working on a certain project to generate pie charts, I am using google charts with an HTML API.

I managed to produce the chart and get the image URL.
That when I call this line:
`window.open(URL);`
a new window is opened containing the picture.
My question is, is there a similar type method that takes the URL and downloads the image in the project folder?
something like `download(URL,'PNG');` or even `download(URL);`?

Answer: 
Read this article <http://pixelscommander.com/en/javascript/javascript-file-download-ignore-content-type/> (you can see working here <http://codepen.io/jelmerdemaat/pen/brjKG?editors=0010>)

You can call `downloadFile(URL);` the user can choose where him will save the image
The answer to question "is there a similar type method that takes the URL and downloads the image in the project folder?" is:

No.

The reason is that any web page containing Javascript is not executed on the server where it is stored: instead, a copy is sent to the user browser, which runs it on user PC; this copy is not aware of the project folder on the server. The only way to save on the server an image created locally on user PC is to upload the image to the server, where for example a .PHP script waits for the data; such PHP script runs on the server, hence it can store in the project folder the received image.

This javascript snippet sends image data to remote PHP file:

```
$.post("http://www.example.com/image-saver.php",
{
  imgdata: imgData,
  pass: "mypass"
}

```

But at the moment the script is called, the image must be already present in the page, which takes a little to be loaded, hence above script must be enclosed inside a function which is activated only after page has completed loading:

```
window.onload = function() {
        $.post("http://www.example.com/image-saver.php",
        {
          imgdata: imgData,
          pass: "mypass"
        }
};

```

The "imgData" can be for example created by converting to URL the data contained in the canvas which holds the image:

```
imgData = canvas.toDataURL('image/png');

```

Hence the full source in the specific case of "php" and "toDataURL" would be:

```
<script src="jquery.js"></script>
<script>
        window.onload = function() {
        imgData = canvas.toDataURL('image/png');
        $.post("http://www.example.com/image-saver.php",
        {
          imgdata: imgData,
          pass: "mypass"
        }
};
</script>

```

These data will be read by image-saver.php script on the server:

```
$data = $_POST['imgdata'];
$pass = $_POST['pass'];

if ($pass == "mypass") {
    $img = imagecreatefromstring(base64_decode(substr($data,strpos($data,',')+1)));
    $result = imagepng($img,"image.png");
    die($img);
    echo "Result: " . $result . "<br>";
} else {
    echo "Nice attempt.";
}

```