附件上传功能

本文档详细介绍了如何在后台框架jeesite和前台UI库miniui的环境下实现附件上传功能。包括html和js代码,以及控制器代码。通过miniui的fileupload组件,实现了文件选择、上传、成功提示、错误处理、删除和下载等功能。同时,控制器中处理了文件上传,保存到服务器,并返回文件ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台框架使用jeesite、前台使用miniui。
html代码:(这里直接使用了miniui的fileupload组件)

附件:

js代码:
//附件上传
function onFileSelect(e) {
//alert(“选择文件”);
}
function onUploadSuccess(e) {
var file = e.file;
var renderResult = JSON.parse(e.serverData)
if(renderResult.result){
fileList.push({fileId:renderResult.data, fileName:file.name})
showTips(‘上传成功!’, ‘success’);
}
this.setText("");
eachFileList();
}
function onUploadError(e) {

}

function startUpload() {
if(fileList.length > 0){
showTips(‘只能上传一个附件!’, ‘success’);
} else {
var fileupload = mini.get(“fileupload1”);
fileupload.startUpload();
}
}
function eachFileList(){
$("#fileList").empty();
var tbody = “”;
//下面使用each进行遍历
$.each(fileList,function(i,obj) {
var lis = “”;
lis += “<li style=“line-height: 26px;”>”+ obj.fileName +"<button style=“margin-left: 20px;” " +
“οnclick=“deleteFile(’”+ obj.fileId +”’,’"+ i +"’)">删除" +
“<a style=“margin-left: 20px;” href=’/a/notify/notify/downloadFile?fileEntity.fileId=”+ obj.fileId +"’>下载";
tbody += lis;
});
$("#fileList").append(tbody);
}
function deleteFile(id,index){
$.ajax({
method: ‘GET’,
url: ‘/a/notify/notify/deleteFile?id=’ + id
}).done(function (result) {
fileList.splice(index, 1);
eachFileList();
showTips(‘删除成功!’, ‘success’);
});
}

controller代码:
/**
* 上传附件
*/
@PostMapping(value = “upload”)
@ResponseBody
public String upload(Notify notify, HttpServletRequest request, HttpServletResponse response) {
MultipartFile uploadFile = null;
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest)request;
Iterator fileIterator = mrequest.getFileNames();
if (fileIterator.hasNext()) {
uploadFile = mrequest.getFile((String)fileIterator.next());
}
}
String fileName = uploadFile.getOriginalFilename();
FileUpload fileUpload = new FileUpload();
fileUpload.preInsert();
FileEntity fileEntity = new FileEntity();
fileEntity.preInsert();
fileUpload.setFileEntity(fileEntity);
fileUpload.setFileName(fileName);
fileUpload.setFileType(“file”);
notifyService.insertFileUpload(fileUpload);
try {
SaveFileFromInputStream(uploadFile.getInputStream(),“E:/fileUpload”,fileEntity.getFileId()); //用fileEntity.getFileId()作为附件的名称,防止文件重名
} catch (IOException e) {
e.printStackTrace();
return renderResult(Global.TRUE, text(“上传出错!”));
}
return renderResult(Global.TRUE, text(“上传成功!”), fileEntity.getFileId());
}

public void SaveFileFromInputStream(InputStream stream, String path, String filename) throws IOException {
	FileOutputStream fs=new FileOutputStream( path + "/" + filename);
	byte[] buffer = new byte[1024*1024];
    int bytesum = 0;
	int byteread = 0;
	while ((byteread=stream.read(buffer))!=-1){
		bytesum+=byteread;
		fs.write(buffer,0,byteread);
		fs.flush();
	}
	fs.close();
	stream.close();
}

/**
 * 附件下载
 * @param fileUpload
 * @param request
 * @param response
 */
@RequestMapping(value = "downloadFile")
public void downloadFile(FileUpload fileUpload,HttpServletRequest request, HttpServletResponse response) {
	String fileId = fileUpload.getFileEntity().getFileId();
	BufferedInputStream in = null;
	BufferedOutputStream out = null;
	FileInputStream fis=null;
	try {
		/*根据id单条查询*/
		fileUpload = notifyService.getFileUpload(fileUpload);
		response.setContentType("text/html;charset=UTF-8");
		//String rootpath = request.getSession().getServletContext().getRealPath("/");
		String rootpath = "E:/fileUpload";//配置文件中指定下载文件路径。例D:\files
		File f = new File(rootpath + File.separator + fileId);
		response.reset();
		response.setContentType("application/x-excel");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ new String(fileUpload.getFileName().getBytes("gbk"),"iso-8859-1"));
		response.setHeader("Content-Length",String.valueOf(f.length()));
		fis=new FileInputStream(f);
		in = new BufferedInputStream(fis);
		out = new BufferedOutputStream(response.getOutputStream());
		byte[] data = new byte[1024];
		int len = 0;
		while (-1 != (len=in.read(data, 0, data.length))) {
			out.write(data, 0, len);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
			}
		}
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
			}
		}
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值