2个回答
展开全部
1首先是jsp
<button class="btn button uploadAndNext" id="importWhiteBtn" type="button">
<span class="button">提交导入</span>
</button>
2.然后是js
$(function(){
var uploadCom = null;
function uploadIt() {
$("#importWhiteBtn").click(function() {
var uploadId = "#" + $(this).attr("id");
var btn = $(this);
uploadCom = new AjaxUpload( uploadId + "" , {
action : "whitePhoneNumber.do?action=importFile",
onSubmit: function(file, ext) {
if ((ext && /^(txt)$/.test(ext))) {
alert("请先下载导入模板readme.txt参考,您上传的文档格式不对,请重新选择!");
return false;
}
$.blockUI({
message:"正在上传到服务器...."
})
},
onComplete: function(file, response) {
$.unblockUI();
if(response=="success"){
alert( "导入成功");
$('#whitePhoneList').flexReload();
}else{
alert( "导入失败,"+response);
}
}
});
});
$(".uploadAndNext").click();
}
uploadIt();
firstClick = false;
})
3.最后是action
public ActionForward importFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String success = "success";
BufferedReader br=null;
InputStream in=null;
String operator = UserNameUtil.getUserName(request);
String areaCode = whitePhoneManager.getAreaCodeByOperator(operator);
try {
List<FileItem> list = upload.parseRequest(request);
for (int i = 0; i < list.size(); i++) {
FileItem fileItem = list.get(i);
String name = fileItem.getName().substring(
fileItem.getName().lastIndexOf("\\") + 1);
if(fileItem.getSize() > 2097152){//200k
throw new Exception(" 文件大小超过2M");
}
log.debug("upload file name:" + name);
in= fileItem.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
whitePhoneManager.processFile(br,areaCode);
logutil.log_Operation(request, LogUtil.LOG_MODULE_SYSTEMCONFIG,
LogUtil.LOG_EVENT_CREATE, "批量导入短信白名单", "filename: "
+ name, "成功",
LogUtil.SUCCESS);
}
} catch (FileUploadException e) {
log.debug("error", e);
success=e.getMessage() ;
} catch (Exception e) {
log.debug("error", e);
success=e.getMessage() ;
}finally{
if(br!=null) try{br.close();}catch(Exception e){log.error("unable to close bufferdreader",e);}
if(in!=null) try{in.close();}catch(Exception e){log.error("unable to close inputstream",e);}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write( success );
return null;
}
}
<button class="btn button uploadAndNext" id="importWhiteBtn" type="button">
<span class="button">提交导入</span>
</button>
2.然后是js
$(function(){
var uploadCom = null;
function uploadIt() {
$("#importWhiteBtn").click(function() {
var uploadId = "#" + $(this).attr("id");
var btn = $(this);
uploadCom = new AjaxUpload( uploadId + "" , {
action : "whitePhoneNumber.do?action=importFile",
onSubmit: function(file, ext) {
if ((ext && /^(txt)$/.test(ext))) {
alert("请先下载导入模板readme.txt参考,您上传的文档格式不对,请重新选择!");
return false;
}
$.blockUI({
message:"正在上传到服务器...."
})
},
onComplete: function(file, response) {
$.unblockUI();
if(response=="success"){
alert( "导入成功");
$('#whitePhoneList').flexReload();
}else{
alert( "导入失败,"+response);
}
}
});
});
$(".uploadAndNext").click();
}
uploadIt();
firstClick = false;
})
3.最后是action
public ActionForward importFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String success = "success";
BufferedReader br=null;
InputStream in=null;
String operator = UserNameUtil.getUserName(request);
String areaCode = whitePhoneManager.getAreaCodeByOperator(operator);
try {
List<FileItem> list = upload.parseRequest(request);
for (int i = 0; i < list.size(); i++) {
FileItem fileItem = list.get(i);
String name = fileItem.getName().substring(
fileItem.getName().lastIndexOf("\\") + 1);
if(fileItem.getSize() > 2097152){//200k
throw new Exception(" 文件大小超过2M");
}
log.debug("upload file name:" + name);
in= fileItem.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
whitePhoneManager.processFile(br,areaCode);
logutil.log_Operation(request, LogUtil.LOG_MODULE_SYSTEMCONFIG,
LogUtil.LOG_EVENT_CREATE, "批量导入短信白名单", "filename: "
+ name, "成功",
LogUtil.SUCCESS);
}
} catch (FileUploadException e) {
log.debug("error", e);
success=e.getMessage() ;
} catch (Exception e) {
log.debug("error", e);
success=e.getMessage() ;
}finally{
if(br!=null) try{br.close();}catch(Exception e){log.error("unable to close bufferdreader",e);}
if(in!=null) try{in.close();}catch(Exception e){log.error("unable to close inputstream",e);}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write( success );
return null;
}
}
追问
。net ,前台jquery 获取完路径。 怎么用
展开全部
上传时是由php完成的,那也就是说jquery uploadify会向一个php页面进行请求,请求就会有返回值,将上传完成后的指定的路径跟名字返回即可
比如:
'onComplete' :function(event, queueID, fileObj, reposnse, data) {
$("#upImg").val(reposnse);
}
这里就是在请求php完成后函数返回值,其中reposnse就是php返回的结果,$("#upImg").val(reposnse);我这里将返回的路径名字给id为upImg的隐藏域里,方便进行表单提交
php的返回值就是echo的内容,比如请求的upload.php上传完成后我指定的路径跟名字赋给变量$val,那我只需要在upload.php最后echo $val;
比如:
'onComplete' :function(event, queueID, fileObj, reposnse, data) {
$("#upImg").val(reposnse);
}
这里就是在请求php完成后函数返回值,其中reposnse就是php返回的结果,$("#upImg").val(reposnse);我这里将返回的路径名字给id为upImg的隐藏域里,方便进行表单提交
php的返回值就是echo的内容,比如请求的upload.php上传完成后我指定的路径跟名字赋给变量$val,那我只需要在upload.php最后echo $val;
追问
。net ,前台jquery 获取完路径。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询