te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Save Image file from Image URL javascript html - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Save Image file from Image URL javascript html - Stack Overflow

programmeradmin3浏览0评论

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);?

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);?

Share Improve this question edited Dec 3, 2019 at 10:48 Sampada 2,9917 gold badges30 silver badges40 bronze badges asked Apr 20, 2016 at 1:28 Michel MinaMichel Mina 1052 gold badges2 silver badges6 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

See this question if you're trying to allow the end user to download that image: Download image with JavaScript

If you're trying to download the image for use in the page, why not use an tag and reference it remotely?

    <img src="[url retrieved from the api]">

Keep in mind that Javascript is run on the client side, so any download functions would download to the end user, not the project folder.

EDIT: Made a fiddle with the code used and made an implementation.

The fiddle.

I pulled from the Google Chart docs "Printing PNGs". Here's how they did it:

//Your data and options initialization up here 
....

//Event listener
google.visualization.events.addListener(chart, 'ready', function () {
    chart_div.innerHTML = '<img src="' + chart.getImageURI() + '" download="chart.png">';
    console.log(chart_div.innerHTML);
}); 

//Actually draw the chart
chart.draw(data, options);

Just use the other SO link to plete the download procedure!

As far as I'm aware there is no way of doing this in Javascript (I could be totally wrong) so you could do it in HTML5 instead.

HTML5:

<a href="website./imageurl.png" download="ImageNameHere">click here</a>

You can get a list of supported browsers for this tag here: http://caniuse./#feat=download

Read this article http://pixelsmander./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./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 pleted loading:

window.onload = function() {
        $.post("http://www.example./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./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.";
}
发布评论

评论列表(0)

  1. 暂无评论