file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!
其實(shí)我也不太清除具體是什么原因,但是后面就可以了!!!
我用的是springMVC 自帶的文件上傳
1、首先肯定是要有springMVC上傳文件的相關(guān)配置!
2、前端
這是動(dòng)態(tài)input file上傳到后臺(tái)沒反應(yīng)的寫法(頁(yè)面上寫死的上傳到后臺(tái)是可以的)
這段代碼是寫在table>>下的form表單里的
改為
吧上面那段代碼換種方式,先寫
,在寫--%>
這樣就可以了,說實(shí)話我也不知道為什么(!!!!)
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 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();
//保存該活動(dòng)貼的相關(guān)信息,材料提交狀態(tài)改為1
topicService.insertIno(topicId,1,peopleCount,perPrice,dataTime);
//如果只是上傳一個(gè)文件,則只需要MultipartFile類型接收文件即可,而且無需顯式指定@RequestParam注解
//如果想上傳多個(gè)文件,那么這里就要用MultipartFile[]類型來接收文件,并且還要指定@RequestParam注解
//并且上傳多個(gè)文件時(shí),前臺(tái)表單中的所有的name都應(yīng)該是myfiles,否則參數(shù)里的myfiles無法獲取到所有上傳的文件
//判斷file數(shù)組不能為空并且長(zhǎng)度大于0
if(myfiles!=null&&myfiles.length>0){
//循環(huán)獲取file數(shù)組中得文件
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();
}
// 轉(zhuǎn)存文件
file.transferTo(targetFile);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
總結(jié)
以上是生活随笔為你收集整理的file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 房子降价会扰乱房市秩序么?
- 下一篇: 怎样选购适合自己的地毯?