其实js下载的本质无非就是让浏览器接收一个流文件。一般前端下载功能可以直接实现,就是html里面有专门的下载标签。匹配属性即可,但有时基于反爬虫、安全考虑或用户需求需要下载触发,是未知结果等等。这时候就需要js来解决了。 最近遇到了类似的问题。写一个引用集合记录直接打开方法var url = "https://xxxxx.txt" //下载地址window . open(URL);local window . location . href = " https://xxxxx . txt "创建表单下载,并通过var url = "https://xxxxx.txt" //下载地址var params = ' 123 '//parameters//创建表单表单var form html = " < form method = ' post ' style = ' display:none ' id = ' form downid ' action = ' "+URL+" & gt;"+" & lt;输入类型= ' hidden ' value = ' "+params+" ' & gt;& lt/form & gt;" var init html = document . createelement(" div ")init html . innerhtml = form html//将表单form放到指定位置document . body . append child(init html)//操作dom,下载文件document . getelementbyid(" formdowntoid ")。submit()//下载完成,删除form document . body . remove child(init HTML)并创建一个标签。下载var url = "https://xxxxx.txt" 1。Click const A = document . createelement(' A ')//A创建标签a.setattribute ('download ',' ')a.setattribute ('href ',Url)a.click() //执行click事件2,通过属性const htmla = document . createelement(' A ')htmla . set attribute(" href ",Url)htmla . set attribute(' download ',' ')const its arr = document . create event(' mouse event ')it arr . init mouse event(" click ",true,true,window,0,0