PHP异步下载
2016-07-18 14:36 小黄瓜啦啦啦 阅读(608) 评论(0) 收藏 举报看到异步,我们第一时间想到的一般都是ajax,通过ajax异步发送一个http request来请求下载页面,但是ajax通常只能返回txt, xml, json等字符串,无法实现下载的操作。本文介绍一种将form提交到隐藏的iframe中,来实现异步下载的方法。方法的原理很简单,就是通过控制form标签的target属性,将表单提交到一个隐藏的iframe标签中,这样就能达到页面无刷新下载的目的。示例代码如下:
<html> <head> <meta charset="UTF-8" /> <script> function download() { //创建一个form,并提交到隐藏的frame中 var form = document.createElement("form"); form.action = "下载页面地址"; form.method = "get"; form.target = "exportFrame"; //参数提交 var input = document.createElement("input"); input.type = "hidden"; input.name = "param"; input.value = document.getElementById("param").value; form.appendChild(input); document.body.appendChild(form); //表单提交 form.submit(); } </script> </head> <body> <input type="text" name="param" id="param" value="" /> <input type="button" onclick="download()" value="下载" /> <!-- 隐藏iframe,将form提交到此frame,达到异步提交的效果 --> <iframe name="exportFrame" width="0" height="0" style="display: none;"></iframe> </body>
浙公网安备 33010602011771号