日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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注解

//并且上傳多個文件時,前臺表單中的所有的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;

}

}

總結

以上是生活随笔為你收集整理的file input 点击没反应_动态input file多文件上传到后台没反应的解决方法!!!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。