详细解决方案
iframe实现无刷新下传文件
热度:95 发布时间:2012-09-24 13:49:41.0
iframe实现无刷新上传文件
[代码] [HTML]代码
04 |
<form action="upload.jsp" id="form1" name="form1" encType="multipart/form-data"? method="post" target="hidden_frame" >??? |
05 |
???? <input type="file" id="file" name="file" style="width:450">???
|
06 |
???? <INPUT type="submit" value="上传文件"><span id="msg"></span>???
|
08 |
???? <font color="red">支持JPG,JPEG,GIF,BMP,SWF,RMVB,RM,AVI文件的上传</font>?????????????????
|
09 |
???? <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>???
|
15 |
<script type="text/javascript">??? |
16 |
function callback(msg)??? |
18 |
???? document.getElementById("file").outerHTML = document.getElementById("file").outerHTML;???
|
19 |
???? document.getElementById("msg").innerHTML = "<font color=red>"+msg+"</font>";???
|
[代码] 原理就是输出一段js代码到 iframe 中,然后在iframe中来控制它的父页面
01 |
<%@ page language= "java" contentType= "text/html; charset=gb2312" %>???
|
02 |
<%@ page import = "com.jspsmart.upload.SmartUpload" %>???
|
06 |
???? SmartUpload su = new SmartUpload();???
|
09 |
???? su.initialize(pageContext);???
|
13 |
???? su.setMaxFileSize( 10000000 );???
|
16 |
???? su.setTotalMaxFileSize( 20000000 );???
|
19 |
???? su.setAllowedFilesList( "doc,txt,jpg,rar,mid,waw,mp3,gif" );???
|
21 |
???? boolean sign = true ;???
|
25 |
???????? su.setDeniedFilesList( "exe,bat,jsp,htm,html" );???
|
28 |
???????? su.upload();???
|
30 |
???????? su.save( "c:\\" );??
|
32 |
???? } catch (Exception e) {??
|
33 |
???????? e.printStackTrace();??
|
34 |
???????? sign = false ;??
|
38 |
???????? out.println( "<script>parent.callback('upload file success')</script>" );??
|
41 |
???????? out.println( "<script>parent.callback('upload file error')</script>" );???
|