- 具体效果:
- 效果描述:产品线下拉框,是动态获取的,根据用户属于哪几个产品线来展示下拉框(用户与产品线关系是一对多)
- html部分:(css未提供)需要引入jquery
<div class="content">
<div class="row">
<span>标题</span>
<input id="title" type="text">
</div>
<div class="row">
<span>产品线</span>
<select name="" id="fileLineid">
<option id="fileLineid1" value="1" style="display:none">产品线1</option>
<option id="fileLineid2" value="2" style="display:none">产品线2</option>
<option id="fileLineid3" value="3" style="display:none">产品线3</option>
<option id="fileLineid4" value="4" style="display:none">产品线4</option>
<option id="fileLineid5" value="5" style="display:none">产品线5</option>
<option id="fileLineid6" value="6" style="display:none">产品线6</option>
<option id="fileLineid7" value="7" style="display:none">产品线7</option>
</select>
</div>
<div class="row">
<span>文档属性</span>
<select name="" id="openFlags">
<option value="1" selected>全公开</option>
<option value="0">产品线内公开</option>
</select>
</div>
<div class="row">
<span>类别</span>
<select name="" id="typeId">
<option value="1" selected>全公开</option>
<option value="0">产品线内公开</option>
</select>
</div>
<div class="row">
<span>简介</span>
<textarea name="" id="abstr" cols="30" rows="10"></textarea>
</div>
<div class="file-upload">
<span>附件上传</span>
<a href="javascript:;" class="upload">+ 选择文件
<input type="file" name="file" id="files" multiple="multiple">
</a>
<input type="submit" value="确认发布" onclick="newDocument()">
</div>
</div>
4.js部分---根据用户属于哪几个产品线>>动态展示产品线下拉框
注:data是controller层传的arrayList<String>数组
$.ajax({
type: "post",
url: "url",
dataType:"json",
contentType:false,
processData:false,
success : function(data) {
console.log(data);
$.each(
data,
function(i,a){ //循环判断哪个让产品线显示
if(data[i] == document.getElementById("fileLineid1").value){
$("#fileLineid1").show();
}
if(data[i] == document.getElementById("fileLineid2").value){
$("#fileLineid2").show();
}
if(data[i] == document.getElementById("fileLineid3").value){
$("#fileLineid3").show();
}
if(data[i] == document.getElementById("fileLineid4").value){
$("#fileLineid4").show();
}
if(data[i] == document.getElementById("fileLineid5").value){
$("#fileLineid5").show();
}
if(data[i] == document.getElementById("fileLineid6").value){
$("#fileLineid6").show();
}
if(data[i] == document.getElementById("fileLineid7").value){
$("#fileLineid7").show();
}
});
}
});
js部分:上传文件到后端controller层
//点击提交按钮触发事件
function newDocument() {
var title = document.getElementById("title").value; //获取文件标题
var fileLineid= document.getElementById("fileLineid").value;//获取当前选择项的值.
var openFlag = document.getElementById("openFlags").value; //
var typeId = document.getElementById("typeId").value; //获取类别id
var abstr = document.getElementById("abstr").value; //获取描述
var formData = new FormData();
for(var i=0;i<$("#files")[0].files.length;i++) { //循环获取上传多少个文件
formData.append("file", $("#files")[0].files[i]);
}
formData.append("title",title);
formData.append("fileLineid",fileLineid);
formData.append("openFlag",openFlag);
formData.append("typeId",typeId);
formData.append("abstr",abstr);
console.log("from是:"+JSON.stringify(formData));
$.ajax({
type: "post",
url: "url",
data : formData,
dataType:"json",
contentType : false, //上传文件一定要是false
//取消帮我们格式化数据,是什么就是什么
processData:false,
success : function() {
console.log("完美");
}
});
}
controller层:(1.接收获取的数据 2.将文件存入电脑的指定目录下 3.存入电脑的文件根据年、月、日建立文件夹 4.存入电脑的文件名称根据时分秒创建文件 5.将文件真实名称 存入电脑的名称 及路径及数据存入数据库中 )
注:创建文件时,速度比较快 ,所以存入电脑的文件名称为:拼接了一个i
//给文件重新命名 当前时间 日:小时:分:秒+后缀
fileName = simpleDateFormat1.format(date)+i+fileRealName1.substring(fileRealName1.lastIndexOf("."));
/**
* @author
* 上传文档接口
*/
@RequestMapping(value="/insertFile",method = RequestMethod.POST)
@ResponseBody
public void insertFile(
@RequestParam("title") String title, //上传文档标题
@RequestParam("openFlag")String openFlags,//文档属性(全公开/线内公开)
@RequestParam("fileLineid") String lineIds, //产品线
@RequestParam("typeId") String typeIds, //文档类型
@RequestParam("abstr") String abstr,//文档摘要
@RequestParam("file") MultipartFile[] files,
HttpServletRequest request, HttpServletResponse response) throws Exception{
int openFlag = Integer.valueOf(openFlags).intValue();
int lineId = Integer.valueOf(lineIds).intValue();
int typeId = Integer.valueOf(typeIds).intValue();
Date date =new Date();
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd:hh:mm:ss");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("hhmmss");//文件唯一时间戳 (名称)
String createTime = simpleDateFormat.format(date);//创建时间
String fileRealName = "";//附件真实名称(放在一起用 分号 隔开)带文件后缀
String fileName = "";//文档名称 (只有名字)
String fileName1 = ""; //多文件放入数据库的名称的集合
String filePath = "";//附件地止
String month = (cal.get(Calendar.MONTH)+1)+"";
String year = (cal.get(Calendar.YEAR))+"";
String day = (cal.get(Calendar.DAY_OF_MONTH))+"";
//判断年份 月份 日文件夹是否已经创建
File newFile = new File("F:\\"+year+"\\"+month+"\\"+day);
newFile.mkdirs();//判断年 月 日文件夹是否创建,没有则新建
//判断上传文件数目是否为多个 及 是否为空
for (int i=0;i<files.length;i++){ //遍历文件
System.out.println("*************");
String fileRealName1 = files[i].getOriginalFilename(); //获取文件 全 名称
fileRealName += fileRealName1 + ";"; //获取文件 全 名称(拼接)
//将文件放入指定路径
//路径整合到一起
System.out.println(fileRealName);
InputStream in = null;
try {
//给文件重新命名 当前时间 日:小时:分:秒+后缀
fileName = simpleDateFormat1.format(date)+i+fileRealName1.substring(fileRealName1.lastIndexOf("."));
System.out.println("文件名称:"+fileName);
fileName1 += fileName+";";
in = files[i].getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
String path="F:\\"+year+"\\"+month+"\\"+day+"\\"+fileName;
filePath += path + ";";
File file1 = new File(path);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();//创建父级文件路径
file1.createNewFile();//创建文件
System.out.println(file1.exists());
}
FileOutputStream out = new FileOutputStream(path);
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
} catch (Exception e) {
System.out.println("---------文件上传失败————————");
e.printStackTrace();
//return false;
}
}
try {
System.out.println("title:"+title+" openflag:"+openFlag+" lineId:"+lineId+" typeId:"+typeId+" abstr:"+abstr+" createTime:"+createTime);
documentService.insertFile(title , openFlag ,lineId ,typeId ,abstr ,createTime ,fileRealName ,fileName1 ,filePath); //将文件信息存入数据库
//return bln;
}catch (Exception e){
//return false;
}
}
各位读者觉得本文哪里需要改正、添加的东西或者疑惑可以留言,互相进步