我项目有这个需求,多文件上传,而且要及时显示到表单上,这样的话就不能与表单相关。

由于我对前端不熟,我就实现了这么一个功能,通过button触发一个div漂浮块,然后多文件上传,之后通过js把文件名显示到下拉列表。

多文件上传采用的是plupload插件。

 <style>
* {
margin:;
padding:;
list-style-type: none;
} body {
font: 12px/180% Arial, Helvetica, sans-serif, "宋体";
} a,img {
border:;
} a {
color: #5e5e5e;
text-decoration: none;
} a:hover {
color: #3366cc;
text-decoration: underline;
}
/* box */
.box {
position: absolute;
width: 800px;
left: 50%;
height: auto;
z-index:;
background-color: #fff;
border: 1px #8FA4F5 solid;
padding: 1px;
} .box h2 {
height: 25px;
font-size: 14px;
background-color: #3366cc;
position: relative;
padding-left: 10px;
line-height: 25px;
color: #fff;
} .box h2 a {
position: absolute;
right: 5px;
font-size: 12px;
color: #fff;
} .box .mainlist {
padding: 10px;
} .box .mainlist li {
height: 24px;
line-height: 24px;
} .box .mainlist li span {
margin: 0 5px 0 0;
font-family: "宋体";
font-size: 12px;
font-weight:;
color: #ddd;
} #TB_overlayBG {
background-color: #666;
position: absolute;
z-index:;
left:;
top:;
display: none;
width: 100%;
height: 100%;
opacity: 0.5;
filter: alpha(opacity = 50);
-moz-opacity: 0.5;
}
</style>

这个是漂浮块样式,样式是我在资料里找的,不知道为什么会影响全局的样式,希望有人改正。

 <script>
$(function() {
$(".showbox").click(
function() {
$("#TB_overlayBG").css({
display : "block",
height : $(document).height()
});
$(".box").css(
{
left : ($("body").width() - $(".box").width())
/ 2 - 20 + "px",
top : ($(window).height() - $(".box").height())
/ 2 + $(window).scrollTop() + "px",
display : "block"
});
}); $(".close").click(function() {
$("#TB_overlayBG").css("display", "none");
$(".box ").css("display", "none");
}); });
</script>

这个是漂浮块的js部分,不要问我,我真的不会

<div id="TB_overlayBG"></div>
<div class="box" style="display:none">
<h2>
文件上传<a href="#" class="close">关闭</a>
</h2>
<div class="mainlist">
<div style="width:750px; margin:0 auto">
<form>
<div id="uploader">
<p>您的浏览器未安装 Flash, Silverlight, Gears, BrowserPlus 或者支持 HTML5
</p>
</div>
<input value="重新上传" id="Reload" type="button">
</form>
</div>
</div>
</div>

html

这个是html代码,里面<div class="mainlist">里面就是PLupload。由于页面已经有一个表单了,我就把这个写在了form表单外,以免文件上传影响到表单。

 <script type="text/javascript">
$(function() {
function plupload() {
$("#uploader")
.pluploadQueue(
{
runtimes : 'html5,gears,browserplus,silverlight,flash,html4',
url : 'fileUploadAction_upload',
max_file_size : '10mb',
unique_names : true,
chunk_size : '2mb',
// Specify what files to browse for
filters : [ {
title : "Image files",
extensions : "doc,docx,txt"
}, {
title : "Zip files",
extensions : "zip"
} ],
resize : {
width : 640,
height : 480,
quality : 90
},
// Flash settings
flash_swf_url : 'fileupload/Moxie.swf',
// Silverlight settings
silverlight_xap_url : 'fileupload/Moxie.xap',
});
}
plupload();
$('#Reload').click(function() {
plupload();
});
});
</script>

这个是文件上传的js部分,我就直接仍在了div的下面,这个东西自己百度plupload api自己看,我用的话直接拿过来就可以用。

<input class="showbox" type="button" value="上传附件" name="B10"
onclick="javascript:void(0);">

这个是button按钮,注意它的样式和onclick事件,不可更改。

需要引入什么js文件我就不贴出来了,百度plupload自己找一找前台的例子,把那些js引入就好。

注意:如果功能出不来,绝大部分可能是因为js文件引错了,或者没引入。

plupload插件是个很有意思的东西,去下载plupload的插件包,找到jquery.plupload.queue文件夹里的jquery.plupload.queue.js

找到以下的这部分

 uploader.bind('FileUploaded', function(up, file) {
handleStatus(file);
});

我用试验证明了,这个方法,每上传一个文件调用一次,多文件上传会分成一个一个的文件单次调用,在这里可以加入自己的方法,把file这个东西引入进去,file.id和file.name就会获取到文件的文件名。

 public class FileUploadAction extends ActionSupport {

     private static final long serialVersionUID = 1L;

     private static final int BUFFER_SIZE = 2 * 1024;

     private int id = -1;

     private AccessoryService accessoryService;
private File file;
private String name;
private List<String> names;
private String fileFileName;
private String fileContentType; private int chunk;
private int chunks; private String result; private void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
if (dst.exists()) {
out = new BufferedOutputStream(new FileOutputStream(dst, true),
BUFFER_SIZE);
} else {
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
}
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public String upload() throws Exception {
String dstPath = "F:/file/" + this.getFileFileName();
File dstFile = new File(dstPath); // 文件已存在(上传了同名的文件)
if (chunk == 0 && dstFile.exists()) {
dstFile.delete();
dstFile = new File(dstPath);
} copy(this.file, dstFile);
System.out.println("上传文件:" + fileFileName + " 临时文件名:"
+ fileContentType + " " + chunk + " " + chunks);
ArrayList<String> al = new ArrayList<String>();
al.add(fileFileName);
if (chunk == chunks - 1) {
//完成一整个文件;
}
return null;
} public String submit() {
String filePath = "D:/";
System.out.println("保存文件路径: " + filePath); HttpServletRequest request = ServletActionContext.getRequest(); result = "";
int count = Integer.parseInt(request.getParameter("uploader_count"));
for (int i = 0; i < count; i++) {
fileFileName = request.getParameter("uploader_" + i + "_name");
name = request.getParameter("uploader_" + i + "_tmpname");
System.out.println(fileFileName + " " + name);
try {
// do something with file;
result += fileFileName + "导入完成. <br/>";
} catch (Exception e) {
result += fileFileName + "导入失败:" + e.getMessage()
+ ". <br/>";
e.printStackTrace();
}
}
return SUCCESS;
} public void setId(int id) {
this.id = id;
} public int getId() {
return id;
} public void setName(String name) {
this.name = name;
} public String getName() {
return name;
} public void setNames(List<String> names) {
this.names = names;
} public List<String> getNames() {
return names;
} 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 int getChunk() {
return chunk;
} public void setChunk(int chunk) {
this.chunk = chunk;
} public int getChunks() {
return chunks;
} public void setChunks(int chunks) {
this.chunks = chunks;
} public void setResult(String result) {
this.result = result;
} public String getResult() {
return result;
} public AccessoryService getAccessoryService() {
return accessoryService;
} public void setAccessoryService(AccessoryService accessoryService) {
this.accessoryService = accessoryService;
}
}

这个后台代码是基于ssh2的,直接拿来就能用。
你百度找到的可能是private File upload;

但是2.X以后版本的属性名就都改成file了,包括fileFileName和fileContentType。

暂时只能想到这么多,有想到什么的给我留言,我想到什么也会写续集

用div漂浮快实现与表单无关的多文件上传功能。的更多相关文章

  1. django项目中form表单和ajax的文件上传功能。

    form表单文件上传 路由 # from表单上传 path('formupload/',apply.formupload,name='formupload/'), 方法 # form表单文件上传 de ...

  2. ajax提交表单、ajax实现文件上传

    ajax提交表单.ajax实现文件上传,有需要的朋友可以参考下. 方式一:利用from表单的targer属性 + 隐藏的iframe 达到类似效果, 支持提交含有文件和普通数据的复杂表单 方式二:使用 ...

  3. 使用ajax提交form表单,包括ajax文件上传【转载】

    [使用ajax提交form表单,包括ajax文件上传] 前言 转载:作者:https://www.cnblogs.com/zhuxiaojie/p/4783939.html 使用ajax请求数据,很多 ...

  4. 使用ajax提交form表单,包括ajax文件上传 转http://www.cnblogs.com/zhuxiaojie/p/4783939.html

    使用ajax提交form表单,包括ajax文件上传 前言 使用ajax请求数据,很多人都会,比如说: $.post(path,{data:data},function(data){ ... },&qu ...

  5. Yii2表单提交(带文件上传)

    今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...

  6. ANDROID使用MULTIPARTENTITYBUILDER实现类似FORM表单提交方式的文件上传

    最近在做 Android 端文件上传,要求采用 form 表单的方式提交,项目使用的 afinal 框架有文件上传功能,但是始终无法与php写的服务端对接上,无法上传成功.读源码发现:afinal 使 ...

  7. 使用ajax提交form表单,包括ajax文件上传

    前言 使用ajax请求数据,很多人都会,比如说: $.post(path,{data:data},function(data){ ... },"json"); 又或者是这样的aja ...

  8. Java后台使用httpclient入门HttpPost请求(form表单提交,File文件上传和传输Json数据)

    一.HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...

  9. js_ajax模拟form表单提交_多文件上传_支持单个删除

    需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...

随机推荐

  1. 跟我一起做一个vue的小项目(七)

    先看下我们所做项目的效果 这些数据都是我们在data中定义的,不是从后端数据中请求的.那么 接下来我们使用axios渲染数据 npm install axios --save 每个组件里面的数据都不相 ...

  2. php 5.3 iis php_memcache 安装不上

    有的服务器很扯淡,安装了很长时间的php_memcache 扩展 始终安装不上 具体原因不清楚 因为 php_memcache.dll php 官网上只有 最新支持的版本 例如 http://pecl ...

  3. PyCharm软件代码配色和字体设置

    配置效果图: 1.字体设置: 2.tab键设置: 3.代码颜色配置: 注释颜色为: 类名称: 函数: 关键字: 关键字参数: 函数参数: 字符串:

  4. [Array]217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. PHP1.6--数组

    一.数组的键值操作函数 1.array_values() 函数作用是返回数组中所有元素的值,只有一个参数,规定传人给定数组,返回一个包含给定数组中所有值的数组,但不保留键名 被返回的数组将使用顺序的数 ...

  6. jdbc原始连接

    public class JdbcInstrt { public static void main(String[] args) { Connection conn = null; PreparedS ...

  7. JDBC 操作数据库实例

    JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...

  8. 我悲惨的人生,该死的UPX壳,谁能救救我

     一个程序,被加了UPX壳... 结果加壳的人把UPX脱壳的关键参数都给删除掉了,我现在连脱壳都脱不掉... 我从网上下载了UPX最新3.91版本的壳,复制了两个UPX.exe,本来互相加壳和脱壳都没 ...

  9. day38 04-Spring加载配置文件

    Spring的工厂类ApplicationContext有两个子类:ClassPathXmlApplicationConext和FileSystemXmlApplication都可以用来加载配置文件. ...

  10. java简单jdbc查询操作

    所采用的mysql的数据库驱动版本:5.0.8 mysql-connector-java-5.0.8-bin.jar 程序结构图: 表结构: 创表sql: Create Table CREATE TA ...