2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!

file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!

时间:2019-05-03 02:22:20

相关推荐

file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!

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

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

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

2、前端

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

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

改为

吧上面那段代码换种方式,先写

,在写

--%>

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

3、js代码

var attachName = "myfiles";

function addInput(){

createInput(attachName);

$("#fileId").append("

移除");

}

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 mons.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注解

//并且上传多个文件时,前台表单中的所有的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件

//判断file数组不能为空并且长度大于0

if(myfiles!=null&&myfiles.length>0){

//循环获取file数组中得文件

for(int i =0;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;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。