一直以为用ajax可以实现无刷新上传文件,可事实证明是不可能的,至少是目前。
现在用iframe实现无刷新上传文件
原理:在页面中加一iframe <iframe id="link" style="display:none;"></iframe>,把form的变量传递页面指到iframe,即target="link",所有看起来页面没有刷新,实际上iframe实现的刷新,只是看不到
<form action="" method="post" target="link">
...
<iframe id="link" style="display:none;"></iframe>
</form>
从控制器调用js:
echo "<script>parent.sunny('Success!');</script>";
局部刷新示例:
<iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe>
方案一:用iframe的name属性定位
<input type="button" name="Button" value="Button"
onclick="document.frames('ifrmname').location.reload()">
或
<input type="button" name="Button" value="Button"
onclick="document.all.ifrmname.document.location.reload()">
方案二:用iframe的id属性定位
<input type="button" name="Button" value="Button"
onclick="ifrmid.window.location.reload()">
终极方案:当iframe的src为其它网站地址(跨域操作时)
<input type="button" name="Button" value="Button"
onclick="window.open(document.all.ifrmname.src,'ifrmname','')">
现在用iframe实现无刷新上传文件
原理:在页面中加一iframe <iframe id="link" style="display:none;"></iframe>,把form的变量传递页面指到iframe,即target="link",所有看起来页面没有刷新,实际上iframe实现的刷新,只是看不到
<form action="" method="post" target="link">
...
<iframe id="link" style="display:none;"></iframe>
</form>
从控制器调用js:
echo "<script>parent.sunny('Success!');</script>";
局部刷新示例:
<iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe>
方案一:用iframe的name属性定位
<input type="button" name="Button" value="Button"
onclick="document.frames('ifrmname').location.reload()">
或
<input type="button" name="Button" value="Button"
onclick="document.all.ifrmname.document.location.reload()">
方案二:用iframe的id属性定位
<input type="button" name="Button" value="Button"
onclick="ifrmid.window.location.reload()">
终极方案:当iframe的src为其它网站地址(跨域操作时)
<input type="button" name="Button" value="Button"
onclick="window.open(document.all.ifrmname.src,'ifrmname','')">