在上一篇文章中是将文件随表单中其他数据一起提交的,这样在提交一个表单时需要较长的时间,用户体验不是很好。一般网站选择将文件与表单的其他数据分开而单独上传。本文介绍如何利用iframe标签将上传文件模块嵌套在表单中,实现无页面刷新的文件上传。
表单页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <div>
- <div>
- <h3>Frame嵌套的方式上传文件</h3>
- </div>
- <div>
- <form action="" method="post">
- <div>
- <span>文件名:</span>
- <input type="text" name="filename" />
- </div>
- <div>
- <iframe src="FrameFileUpload.jsp"></iframe>
- </div>
- <div>
- <input type="submit" value="提交" />
- </div>
- </form>
- </div>
- </div>
- </body>
- </html>
文件上传页面(通过iframe嵌套在表单页面)
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>File Upload</title>
- </head>
- <body>
- <div>
- <form action="frameFileLoadAction" enctype="multipart/form-data"
- method="post">
- <div>
- <input type="file" name="file" id="file_upload" />
- </div>
- <div>
- <input type="submit" value="上传" />
- </div>
- </form>
- </div>
- <div>
- <span>上传结果:</span>
- <span>
- <%
- String isSuccess = request.getParameter("isSuccess");
- if ("1".equals(isSuccess)) {
- out.print("上传成功");
- } else if ("0".equals(isSuccess)) {
- out.print("上传失败");
- } else {
- out.print("还没有上传文件");
- }
- %>
- </span>
- </div>
- </body>
- </html>
struts2配置文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <constant name="struts.ui.theme" value="simple" />
- <constant name="struts.multipart.maxSize" value="100" />
- <constant name="struts.multipart.saveDir " value="c:/tmp" />
- <package name="fileUpload" extends="struts-default" namespace="/">
- <action name="frameFileLoadAction" class="com.study.action.FileUploadAction"
- method="execute">
- <param name="filePath">/filetemp</param>
- <result name="success">/FrameFileUpload.jsp?isSuccess=1</result>
- <result name="input">/FrameFileUpload.jsp?isSuccess=0</result>
- </action>
- </package>
- </struts>
Struts2的Action
- package com.study.action;
- import java.io.File;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- import com.study.util.FileCopyUtil;
- public class FileUploadAction extends ActionSupport {
- protected File file;
- protected String fileFileName;
- protected String fileContentType;
- protected String filePath;
- protected void restoreFile() throws Exception {
- if (this.file != null) {
- File temp = new File(ServletActionContext.getServletContext()
- .getRealPath(this.filePath) + "/" + this.fileFileName);
- FileCopyUtil.copy(this.file, temp);
- }
- }
- @Override
- public String execute() throws Exception {
- this.restoreFile();
- return "success";
- }
- public File getFile() {
- return file;
- }
- public void setFile(File file) {
- this.file = file;
- }
- public String getFileFileName() {
- return fileFileName;
- }
- public void setFileFileName(String fileFileName) {
- this.fileFileName = fileFileName;
- }
- public String getFileContentType() {
- return fileContentType;
- }
- public void setFileContentType(String fileContentType) {
- this.fileContentType = fileContentType;
- }
- public String getFilePath() {
- return filePath;
- }
- public void setFilePath(String filePath) {
- this.filePath = filePath;
- }
- }
总结:通过iframe嵌套文件上传页面,从而将文件上传与其他数据分开提交。其实刷新的是iframe。但这样整个页面不会刷新,给用户的感觉更好。
PS:上传文件后可以将文件的存储位置返回并放在表单中与其他数据一块上传。