JS怎么保存图片到本地

JavaScript019

JS怎么保存图片到本地,第1张

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")

<script language="javascript">

function SaveAs(href,name)

{

var a = window.open(href)

a.document.execCommand('Saveas',true,name)

a.window.close()

return false

}

</script>

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>实验</title>

</head>

<body>

<a href="1.jpg" onClick="return SaveAs(this.href,'1.jpg')">下载</a>

</body>

</html>