码迷,mamicode.com
首页 > Web开发 > 详细

ajax+MultipartFile上传文件到本地

时间:2018-06-22 13:44:03      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:page   ajax   button   pen   stack   icp   tom   tip   .post   

.html

<th data-options="field:‘op‘,align:‘center‘,width:220,formatter:rmtManager.supportOpFormmater()">操作</th>
<div id="uploadCertificate" >
<div class="easyui-layout" data-options="fit:true">
<div data-options="region:‘center‘" style="padding:10px;">
<div style="margin-bottom:20px">
<form id="excelform" method="post" enctype="multipart/form-data">
<div>请选择要上传的证书:</div>
<input name="certificate" class="easyui-filebox" data-options="required:‘true‘,buttonText:‘选择文件‘" style="width:100%">
<input type="hidden" id="accountId" name="accountId" value="" />
</form>
</div>
</div>
<div data-options="region:‘south‘,border:false" style="text-align:right;padding:5px 0 0;">
<a id="import" href="#" class="easyui-linkbutton" data-options="iconCls:‘icon-ok‘" >上传</a>
<a id="cancel" href="#" class="easyui-linkbutton" data-options="iconCls:‘icon-cancel‘">取消</a>
</div>
</div>
</div>


.js

(function($) {
var pageType= $(‘#glPageType‘).val();
var ctx= $(‘#ctx‘).val();
var editIndex = undefined;
var busMng = rmtCMHelper.createBusMngInst({
busMngOptions:{
                toUpload: function(flag){
$(‘#accountId‘).val(flag);
$(‘#uploadCertificate‘).window({
width:300,
height:150,
title:‘证书导入‘,
minimizable:false,
maximizable:false,
collapsible:false,
modal:true
});
$(‘#uploadCertificate‘).window(‘open‘);
},
       supportOpFormmater: function(){
return function(value,row,index){
return ‘<a href="javascript:void(0);" class="todo" onclick="rmtManager.toToken( ‘+row.id+‘ )">查看TOKEN</a>&nbsp;&nbsp;&nbsp;&nbsp;‘
+ ‘<a href="javascript:void(0);" class="todo" onclick="rmtManager.addConfig( ‘+row.id+‘ )">配置</a>&nbsp;&nbsp;&nbsp;&nbsp;‘
+ ‘<a href="javascript:void(0);" class="todo" onclick="rmtManager.toView( ‘+row.id+‘ )">查看</a>&nbsp;&nbsp;&nbsp;&nbsp;‘
+‘ <a href="javascript:void(0);" class="todo" onclick="rmtManager.toUpload(‘+row.id+‘ )">上传证书</a>&nbsp;&nbsp;&nbsp;&nbsp;‘;
}
}
});
$(‘#import‘).bind(‘click‘, function(){


$(‘#excelform‘).form(‘submit‘, {
url: ctx+"/wxbase/account/upload.html",
onSubmit: function(){
if($(‘#excelform‘).form(‘validate‘))
rmtHelper.showMask();
},
success:function(data){
var data = eval(‘(‘ + data + ‘)‘);
if(data.success){
rmtHelper.hideMask();
$(‘#uploadCertificate‘).window(‘close‘);
rmtHelper.showMsg(data.content);
rmtHelper.getCurCurdIst().reloadGrid();
}else{
rmtHelper.hideMask();
rmtHelper.showMsg(data.content);
}
}
});
});
$(‘#cancel‘).bind(‘click‘, function(){
$(‘#uploadCertificate‘).window(‘close‘);
});
controller


/**
* 上传证书
* @param request
* @return
*/
@RequestMapping(value="upload", method = RequestMethod.POST)
@ResponseBody
public ResultJsonInfo uploadCertificate(@RequestParam("certificate") MultipartFile certificate, HttpServletRequest request)throws Exception {
String accountId = request.getParameter("accountId");
String fileName="";
try {
String picPath ="D://"+ accountId +"/";
//判断之前是否存在证书
File certificateFile = new File(picPath+accountId+".cer");

if(certificateFile.exists()) {
//已存在证书则删除
certificateFile.delete();
}
//根据真实路径创建目录文件
File picSaveFile = new File(picPath);
//判断是否存在路径
if(!picSaveFile.exists()) {
//如果不存在则创建路径
picSaveFile.mkdirs();
}
if (!certificate.isEmpty()) {
try {
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
fileName=accountId+".cer";
FileOutputStream fs=new FileOutputStream( picPath + fileName);
bytesum+=byteread;
fs.write(buffer,0,byteread);
System.out.println("文件流"+certificate.getOriginalFilename());
// 转存文件 */
/*certificate.transferTo(new File(picPath));*/
} catch (Exception e) {
e.printStackTrace();
}
return new ResultJsonInfo(true,"上传成功"+",");
}
}catch (Exception e) {
e.printStackTrace();
return new ResultJsonInfo(false,"上传失败"+",");
}
/*logger.info("导入成功笔数:"+count+",耗时:" + (end - start) + "毫秒");*/
return new ResultJsonInfo(true,"上传成功"+",");
}

紫色部分都要相同命名

ajax+MultipartFile上传文件到本地

标签:page   ajax   button   pen   stack   icp   tom   tip   .post   

原文地址:https://www.cnblogs.com/CatsBlog/p/9212498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!