php,或js如何实现点击图片另存为

JavaScript031

php,或js如何实现点击图片另存为,第1张

JS:

使用JS实现单击连接保存图片2种形式都可以第一种:

<script>

function SaveAs5(imgURL)

{

var oPop = window.open(imgURL,"","width=1, height=1, top=5000,

left=5000")

for(oPop.document.readyState != "complete")

{ if (oPop.document.readyState == "complete")break}

oPop.document.execCommand("SaveAs")oPop.close()

}

</script>

<img src="t_screenshot_17616.jpg" id="DemoImg" border="0"

onclick="SaveAs5(this.src)">

第二种:

<script>function SaveAs5(imgURL)

{

var oPop = window.open(imgURL,"","width=1, height=1, top=5000,

left=5000")

for(oPop.document.readyState != "complete")

{ if (oPop.document.readyState == "complete")break}

oPop.document.execCommand("SaveAs")oPop.close()

}

</script>

<img src="../t_screenshot_17616.jpg" id="DemoImg" border="0">

<a href="#" onclick="SaveAs5(document.getElementByIdx_x('DemoImg').src)">

点击这里下载图片 </a>

PHP:

<a href="test.php?url=uploads/1112/1-11122212201R28.jpg">下载</a>

test.php文件代码:

<?php

$dir=$_GET['url'] //获取图片地址$file = fopen($dir,"r") // 打开文件

Header("Content-type: application/octet-stream")

Header("Accept-Ranges: bytes")

Header("Accept-Length: ".filesize($dir))

Header("Content-Disposition: attachment filename=" . $dir)

echo fread($file,filesize($dir))

fclose($file)

exit

?>

使用JS实现单击连接保存图片

2种形式都可以

第一种:

<script>

function SaveAs5(imgURL)

...{

var oPop = window.open(imgURL,"","width=1, height=1, top=5000, left=5000")

for( oPop.document.readyState != "complete" )

...{

if (oPop.document.readyState == "complete")break

}

oPop.document.execCommand("SaveAs")

oPop.close()

}

</script>

<img src="t_screenshot_17616.jpg" id="DemoImg" border="0" onclick="SaveAs5(this.src)">

第二种:

<script>

function SaveAs5(imgURL)

...{

var oPop = window.open(imgURL,"","width=1, height=1, top=5000, left=5000")

for( oPop.document.readyState != "complete" )

...{

if (oPop.document.readyState == "complete")break

}

oPop.document.execCommand("SaveAs")

oPop.close()

}

</script>

<img src="../t_screenshot_17616.jpg" id="DemoImg" border="0">

<a href="#" onclick="SaveAs5(document.getElementById('DemoImg').src)">点击这里下载图片 </a>

<img src="image\00.jpg" id="select" class="style2"/>

<script>

function test(){

var test1=document.getElementById("select").src

window.alert(test1)

}

test()

</script>

js代码执行应该放图片元素后面。 放在前面的时候,元素还是没有被浏览器加载,因此找不到