其实我也不太清除具体是什么原因,但是后面就可以了!!!

我用的是springMVC 自带的文件上传

1、首先肯定是要有springMVC上传文件的相关配置!

2、前端

  这是动态input file上传到后台没反应的写法(页面上写死的上传到后台是可以的)

  这段代码是写在table>>下的form表单里的

  <input type="button" name="button" value="添加附件" onclick="addInput()">
<input type="button" name="button" value="删除附件" onclick="deleteInput()">
<span id="upload"></span>
改为
吧上面那段代码换种方式,先写<form>,在写<table>

<div class="modal-content" style="width: 600px">
<form action="/ecp/mulFileUpload/testFu" method="POST" enctype="multipart/form-data">
<input type="hidden" name="topicId" value="${topicId}">
<table class="table table-bordered">
<tr>
<th>活动参与人数:</th>
<td><input type="text" name="peopleCount"/></td>
</tr>
<tr>
<th>活动人均经费:</th>
<td><input type="text" name="perPrice"/></td>
</tr>
<tr>
<th>上传图片/附件:</th>
<td>
<input type="file" name="myfiles"/>
<span id="upload"></span>
</td>
</tr>
<tr>
<td>
<input type="button" name="button" value="添加图片/附件" onclick="addInput()">
<input type="button" name="button" value="删除图片/附件" onclick="deleteInput()">
</td>
<%--<td><button class="btn btn-success" onclick="formSubmit()"></button></td>--%>
<td><input type="submit" class="btn btn-success"/></td>
</tr>

</table>
</form>
</div>

这样就可以了,说实话我也不知道为什么(!!!!)

3、js代码

var attachName = "myfiles";

function addInput(){
createInput(attachName);

$("#fileId").append("<div><input style="display:inline;" type="file" name='myfiles'><button onclick="deleteInput(this)">移除</button></div>");

}

function deleteInput(obj){
removeInput();

obj.parentNode.remove();

}

function createInput(name){
var aElement=document.createElement("input");
aElement.name=name;
aElement.type="file";

var spanElement = document.getElementById("upload");
/* if(document.getElementById("upload").insertBefore(aElement,spanElement.nextSibling) == null){
return false;
}*/
if(document.getElementById("upload").appendChild(aElement) == null){
return false;
}
return true;
}

function removeInput(){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null){
return false;
}

return true;
}

4、Java代码

package com.ibm.db.controller;

import com.ibm.db.service.IMulFileUploadService;
import com.ibm.db.service.ITopicService;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created by zml on 16-4-11.
*/
@RestController
@RequestMapping(value = "/ecp/mulFileUpload")
public class mulFileLoadifyController {

@Resource(name = IMulFileUploadService.SERVICE_NAME)
private IMulFileUploadService topicService;

@RequestMapping(value = "/testFu")
public String addUser( @RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException {

Date dataTime = new Date();
//保存该活动贴的相关信息,材料提交状态改为1
topicService.insertIno(topicId,1,peopleCount,perPrice,dataTime);

//如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
//如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
//并且上传多个文件时,前台表单中的所有<input type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
//判断file数组不能为空并且长度大于0
if(myfiles!=null&&myfiles.length>0){

//循环获取file数组中得文件
for(int i =0;i<myfiles.length;i++){
MultipartFile file = myfiles[i];
String uploadContentType =file.getContentType();
String expandedName ="";
if (uploadContentType.equals("imagepeg")
|| uploadContentType.equals("image/jpeg")) {
// IE6上传jpg图片的headimageContentType是imagepeg,而IE9以及火狐上传的jpg图片是image/jpeg
expandedName = ".jpg";
} else if (uploadContentType.equals("image/png")
|| uploadContentType.equals("image/x-png")) {
// IE6上传的png图片的headimageContentType是"image/x-png"
expandedName = ".png";
} else if (uploadContentType.equals("image/gif")) {
expandedName = ".gif";
} else if (uploadContentType.equals("image/bmp")) {
expandedName = ".bmp";
}

//保存文件
saveFile(file,expandedName,request);
}
}
return "uploadSuccess";
//return "redirect:/list.html";
}

/***
* 保存文件
* @param file
* @return
*/
private boolean saveFile(MultipartFile file,String expandedName,HttpServletRequest request) {
DateFormat df = new SimpleDateFormat(TopicController.DEFAULT_SUB_FOLDER_FORMAT_AUTO);
String fileName = df.format(new Date());
// 判断文件是否为空
if (!file.isEmpty()) {
try {
String filePath = "";
// 文件保存路径
if(expandedName!=null&&!expandedName.equals("")){
//如果是图片
filePath = request.getSession().getServletContext().getRealPath("/") + "upload/img/"
+ fileName+expandedName;
}else{
String OriginalFilename = file.getOriginalFilename();
String suffix=OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
filePath = request.getSession().getServletContext().getRealPath("/") + "upload/file/"
+ fileName+"."+suffix;
}

File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}

// 转存文件
file.transferTo(targetFile);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}

 

动态input file多文件上传到后台没反应的解决方法!!!的更多相关文章

  1. win服务器 文件上传下载出现“未指定的错误” 解决方法汇总

    环境 WIN平台IIS服务器   经常出现于ASPX页面 汇总 1.权限问题 出现场景 : 基于ACCESS数据库   原因解析 : 1.首先需要排除自身问题,例如建表使用关键字,格式错误,插入数据与 ...

  2. html input file标签的上传文件 注意点

    文件上传框  代码格式:<input type=“file” name=“...” size=“15” input enctype="multipart/form-data“ maxl ...

  3. jquery的input:type=file实现文件上传

    <!DOCTYPE html> <html> <head> <title>html5_2.html</title> <style> ...

  4. input file标签限制上传文件类型

    用 input 的file类型标签上传文件,有时需要限制上传文件类型,添加accept属性可以实现 <input type="file" accept="image ...

  5. jQuery File Upload文件上传插件简单使用

    前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...

  6. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...

  7. jspsmartupload 文件上传让input数据和文件上传同时提交

    一.使用原因: 文件上传时,表单的属性中必须要有multipart/form-data,如以下例子: <form name="form_post" class="a ...

  8. DVWA之File Upload (文件上传漏洞)

    目录 Low: Medium: 方法一:抓包修改文件的type 方法二:00截断 High: Impossible : Low: 源代码: <?php if( isset( $_POST[ 'U ...

  9. Asp.Net 无刷新文件上传并显示进度条的实现方法及思路

    相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认”拿来主义“,只是我个人更喜欢凡是求个所以 ...

随机推荐

  1. 分页管理的AJAX实现

    bookMgr.jsp <%-- Document : bookMgr.jsp Created on : 2016-11-7, 9:48:21 Author : guanghe --%> ...

  2. docke跨主机通信之gre隧道

    GRE简介 GRE可以对网络层的任何协议来进行封装,类似LVS的IPIP协议,在原有的数据报上增加GRE协议数据报.然后在网络上传输,到达对端后,解开GRE数据报头,得到真实的数据报.其中的mac地址 ...

  3. 有关GPU硬件的理解

    1 显卡的DRAM相当于CPU的RAM (Random access memory, 内存). 二者共同的特点是通电的时候才能使用,不正常断电数据就丢失,但正常情况下,会将数据存储到硬盘中.显存又称帧 ...

  4. CityGML文件格式

    1 LOD3中,wall是由cuboid组成的,一个墙面包括8个面,分为wall-1, wall-2...wall-8,door也是,因此他们都是multisurface (一般由8个面片组成). 在 ...

  5. myeclipse2013和以后版本破解

    当你下到最新版的myeclipse-blue的时候你是否会为注册激活而烦恼呢,别担心,其实激活也就那么点事儿,请遵循我如下做法就可以了: 1.运行jdk下面的cracker.jar文件来打开生成活跃码 ...

  6. HTTP状态码和常用对照表

    http://tool.oschina.net/commons 响应码:“200” : OK: “302” : Found 暂时转移,用于重定向, Response.Redirect()会让浏览器再请 ...

  7. innobackupex err

    [root@12db etc]# innobackupex --user=root /dbback/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyrigh ...

  8. C#学习笔记-----基于AppDomain的"插件式"开发

    很多时候,我们都想使用(开发)USB式(热插拔)的应用,例如,开发一个WinForm应用,并且这个WinForm应用能允许开发人员定制扩展插件,又例如,我们可能维护着一个WinService管理系统, ...

  9. Android常用查询网站

    一.老罗的Android之旅爱生活,爱Android http://blog.csdn.net/luoshengyang 二.Android中文API http://www.android-doc.c ...

  10. Java Hour 13 集合基础

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...