您的位置:首页 > 本地本地
JS 下载/导出 csv、excel、txt 、img等文件的方法总结
admin2024-09-01人已围观
一、JS 下载/导出 csv、excel、txt 、img等文件的方法总结
1. 调用后端接口导出文件
示例下载接口url
1.1 window.open(url)
会打开一个新窗口,开始下载后会自动关闭新窗口。Safair 下载后没有关闭新窗口。
Chrome、IE、Safair支持,貌似火狐不支持
1.2 window.location=url
在当前窗口下载
Chrome、Safair支持
1.3 iframe
在HTML中,iframe 的属性用src,但在JS中,只有部份浏览器支持修改src(读是没问题),真正通用的是要修改对应框架的href值。
1.4 <a href=url download=filename>点击链接下载</a>
HTML5中给a标签增加了一个download属性,只要有这个属性,点击这个链接时浏览器就不在打开链接指向的文件,而是改为下载,目前只有chrome、firefox、opera、Edge支持。常用此方法点击下载图片。
IE既不支持a标签的download属性也不允许js调用a 标签的click方法。
2. 前端直接导出文件到本地
2.1 将数据转成DataURI用<a>标签下载
<a href=DataURI download=filename>点击链接下载</a>
Data URI Scheme
Data URI Scheme是指可以在Web 页面中包含图片但无需任何额外的HTTP 请求的一类URI。 Data URI Scheme一般用于将经过base64编码的数据嵌入网页中,从而减少请求资源的链接数。IE8 之前的版本都不支持 data URI scheme。
DataURI的格式:
生成DataURI的方式
1. encodeURIComponent
使用这种方式,当数据过多时,URI长度容易超出浏览器限制。 encodeURIComponent常用来转码接口参数,为了避免服务器收到不可预知的请求,对任何用户输入的作为URI部分的内容都需要用encodeURIComponent进行转义。
2. URL.createObjectURL
URL.createObjectURL的参数是File对象或者Blob对象
IE10以下不支持URL.createObjectURL
2.2 windows.navigator.msSaveBlob IE10~Edge 专用
msSaveBlob 是IE10~Edge 私有方法。
2.3 execCommand
有的资料有提到IE9可以使用execCommand方法来保存数据到本地文件,但是我自己没有验证过,不知道是否可行。而且MDN文档中execCommand没有查到SaveAs命令。这块只是做个小记录。
js数据直接导出/下载数据到本地到方法总结
本文转载自:
二、求助:HTML点击按钮调用JS文件或者直接调用JS代码?
HTML点击按钮调用JS文件或者直接调用JS代码的方法。
如下参考:
1.将这段代码保存到一个文件中,如下图所示。
2.请注意,您可以使用任何文本工具创建js文件,但是在保存它时,您需要将其后缀为.js。
3.将版本js文件保存为index.js。如果我们需要使用index。js。我们只需要调用它,不需要写一个新的js文件。
4.在HTML的头部部分引入js文件,具体代码如下图所示。
5.引用是一个双标签,所以如果没有内容,就必须完全写入。
6.如果你的js是从外部导入的,你不能在中间写js代码。
7.如果需要,您需要重新声明脚本标记,并在标记中编写js代码,如下图所示。
8.javascriptjquery也是一样的规则,你必须在script标签中写代码。随着js文件的引入,你可以简单地在你想要的地方调用它。
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>test.worker</title>
</head>
<body>
<a href=javascript:test(); class=btn ps-cs6>按钮</a>
<script>
function test()
{
alert(123);
}
</script>
</body>
</html>
记得采纳我的答案哦 , 我是在看到没人回答正确答案的情况下 , 写给你的.也希望你能认可
三、JS如何在当前目录创建txt文件
<script type=text/javascript>
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = thisURL: [ + thisURL + ]<br />
strwrite += thisHREF: [ + thisHREF + ]<br />
strwrite += thisSLoc: [ + thisSLoc + ]<br />
strwrite += thisDLoc: [ + thisDLoc + ]<br />
document.write( strwrite );
</script>
<script type=text/javascript>
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = thisTLoc: [ + thisTLoc + ]<br />
strwrite += thisPLoc: [ + thisPLoc + ]<br />
strwrite += thisTHost: [ + thisTHost + ]<br />
strwrite += thisHost: [ + thisHost + ]<br />
document.write( strwrite );
</script>
<script type=text/javascript>
tmpHPage = thisHREF.split( / );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( / );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = thisHPage: [ + thisHPage + ]<br />
strwrite += thisUPage: [ + thisUPage + ]<br />
document.write( strwrite );
</script>
<%
Public Function ReportFileStatus(FileName)
Dim msg,fso
msg = -1
Set fso = Server.CreateObject(Scripting.FileSystemObject)
If (fso.FileExists(FileName)) Then
msg = 1
Else
msg = -1
End If
ReportFileStatus = msg
End Function
Public Function ReadTxtFile(FileName)
Const ForReading = 1, ForWriting = 2
Dim otf, msg, fso
set fso=CreateObject(Scripting.FileSystemObject)
If ReportFileStatus(FileName) = 1 then
Set otf = fso.OpenTextFile(FileName,ForReading)
if otf.atendofstream <> true then
msg = otf.readAll
ReadTxtFile = msg
otf.Close
Else
ReadTxtFile = -1
End if
End if
End Function
Public Function WriteTxtFile(FileName)
Const ForReading = 1, ForWriting = 2 , ForAppending = 8
Dim fso, msg, ctf, otf
Set fso = Server.CreateObject(Scripting.FileSystemObject)
msg=ReadTxtFile(FileName)
set ctf=fso.CreateTextFile(FileName,true)
ctf.Close
set otf=fso.OpenTextFile(FileName,ForWriting, True)
otf.Write Cstr(Clng(msg)+1)
otf.Close
If ReportFileStatus(FileName) = 1 then
WriteTxtFile = 1
Else
WriteTxtFile = -1
End if
End Function
Function Spath(Rpath)
Spath=Server.MapPath(Rpath)
End Function
WriteTxtFile Spath(count.txt)
%>
这是vbs,是用作网站访问人数统计的,它和js思路一样,只是语法不同,稍微改一下就可以用了,这对你来说是小KS啦。
在开始 程序 附件里有记事本导出的文件就是txt格式
很赞哦! ()
上一篇:手机版饥荒怎么联机麻烦说清楚一点'>谈谈自媒体、新媒体和融媒体
下一篇:返回列表'>返回列表