js存储图片
‘壹’ php,或js如何实现点击图片另存为
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:
<ahref="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);
echofread($file,filesize($dir));
fclose($file);
exit;
?>
‘贰’ js如何将base64格式图片保存到服务器
下面一小段代码给大家分享Nodejs把接收图片base64格式保存为文件存储到服务器上的方法,具体代码如下所示:
app.post('/upload', function(req, res){
//接收前台POST过来的base64
var imgData = req.body.imgData;
//过滤data:URL
var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
var dataBuffer = new Buffer(base64Data, 'base64');
fs.writeFile("image.png", dataBuffer, function(err) {
if(err){
res.send(err);
}else{
res.send("保存成功!");
}
});
});
‘叁’ js中如何将某地址的图片保存到本地指定文件夹中
用JSPSMART处理,参考下面代码实现:
<%
//程序初始化 下面设置成要保存的文件夹。
String path_tmp = request.getRealPath("/") + "Upload";
String filename_p = "Test";
String path_new = request.getRealPath("/") + "Upload\" + filename_p;
//文件上传。
‘肆’ JS怎么保存图片到本地
js没有操作本地文件的权限,可以借助.net,php等后端语言才行的,将图片提交之后,返回个下载地址,window.open就自动下载了。
但是图片可以是svg的话
function saveAs(Url,filename){
var blob=new Blob([''], {type:'application/octet-stream'});
var url = webkitURL.createObjectURL(blob);
var a = document.createElementNS(xhtml,'a');
a.href = Url;
a.download = filename;
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
webkitURL.revokeObjectURL(url);
2.saveAs(data,"new.svg")