web前端|js教程
ajaxSubmit,异步上传
web前端-js教程
(jsp需要引入 :jquery-1.9.0.js、jquery.form.js ) ,jsp页面使用的是bootstrap制作的,看不懂的标签不用管,form表单大同小异。代码比较简陋,只是为了演示使用ajaxSubmit异步上传图片及保存数据,请海含!
(参考文献:/shouce/jquery/jquery_api/Plugins/Form/ajaxSubmit.html)
一:web (add.jsp)
电影资源出售网站源码,ubuntu配置色深,不显示控制台tomcat,各来地方爬虫,php打印金字塔循环,成都seo策划lzw
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
response.setCharacterEncoding("utf-8");//这个是设置编码方式
response.setContentType("text/html");//这个是设置网页类型,为文本代码
UserInformation user=null;
String username="";
Integer userId=null;
if(request.getSession().getAttribute("userinfo")!=null){
user=(UserInformation)request.getSession().getAttribute("userinfo");
username=user.getUsername();
userId=user.getUserId();
}else{
username="请登录";
}
%>
android 文件锁 源码,vscode软件怎么放大字体,nas开源ubuntu,tomcat9配置参数,bw数据爬虫,php网站架设教程,江苏seo全网营销获客平台lzw
手机锁机源码密码,vscode怎么将文件管理,彻底删除ubuntu,tomcat 登录不了了,爬虫丰荣,php md5源码,如何判断seo优化报价合理,在线解析播放网站源码加接口,超级客服生成模板lzw
© |隐私条款|服务条款|粤ICP备1619号-1
/**
*
* V1.0
*/
$(document).ready(function() {
//验证商圈名
$(“#businessName”).blur(function(){
var businessName=$(“#businessName”).val();
if(businessName!=””){
$(“#businessName_error”).html(““);
}else{
$(“#businessName_error”).attr(“class”,”error_div”).html(““+”商圈名不能为空!”);
}
});
//验证商圈英文名
$(“#businessEname”).blur(function(){
var businessEname=$(“#businessEname”).val();
if(businessEname!=””){
$(“#businessEname_error”).html(““);
}else{
$(“#businessEname_error”).attr(“class”,”error_div”).html(““+”商圈英文名不能为空!”);
}
});
//图片上传 及数据保存
$(“#imgSave”).click(function(){
var ext = ‘.jpg.jpeg.jpg.jpg.jpg.’;
var f=$(“#file”).val();
if (f== “”) {//先判断是否已选择了文件
$(“#file_error”).attr(“class”,”error_div”).html(““+”请添加商圈logo!”);
return false;
}
f = f.substr(f.lastIndexOf(‘.’) + 1).toLowerCase();
if (ext.indexOf(‘.’ + f + ‘.’) == -1) {
$(“#file_error”).attr(“class”,”error_div”).html(““+”图片格式不正确!”);
return false;
}
var options = {
url: “${ctx}/upload/upload.do”,
dataType: ‘json’,
contentType: “application/json; charset=utf-8”,
success: function(data) {
// ‘data’ is an object representing the the evaluated json data
// 如果图片上传成功则保存表单注册数据
if(data.status==”0″){
var fileName=data.fileName;
//alert(fileName);
var businessName=$(“#businessName”).val();
var userId=$(“#userId”).val();
var businessEname=$(“#businessEname”).val();
businessName=encodeURI(businessName);
businessName=encodeURI(businessName);
var urls=”${ctx}/business/addBusiness.do?businessName=”+businessName+”&businessPic=”+fileName+”&businessEname=”+businessEname+”&userId=”+userId;
$.ajax({
type: “post”,
url:urls ,
dataType: “json”, /*这句可用可不用,没有影响*/
contentType: “application/json; charset=utf-8”,
success: function (data) {
if(data.status==”0″){
alert(“注册成功!”);
}else{
alert(“注册失败!原因是:”+data.message);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}else{
$(“#file_error”).attr(“class”,”error_div”).html(““+data.message);
}
}
};
// 提交表单
$(‘#uploadImgForm’).ajaxSubmit(options);
});
});
二:service(FileUploadController.java —-springMVC 之controller层)
@Controller
@RequestMapping(value = "/upload")
public class FileUploadController {
private Logger logger;
@RequestMapping(value = "upload.do", method = RequestMethod.POST)
public void fileUpload(HttpServletRequest request, HttpServletResponse response) {
Map resultMap = new HashMap();
String newRealFileName = null;
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
// 获得文件名:
String realFileName = file.getOriginalFilename();
if(file.getSize()/1024>=5*1024){
resultMap.put("status", 1);
resultMap.put("message", "图片不能大于5M");
}else{
System.out.println("获得文件名:" + realFileName);
newRealFileName = FileUploadController.getNowTime() + realFileName.substring(realFileName.indexOf("."));
// 获取路径
String ctxPath = request.getSession().getServletContext().getRealPath("//") + "//temp//";
// 创建文件
File dirPath = new File(ctxPath);
if (!dirPath.exists()) {
dirPath.mkdir();
}
File uploadFile = new File(ctxPath + newRealFileName);
FileCopyUtils.copy(file.getBytes(), uploadFile);
request.setAttribute("files", loadFiles(request));
resultMap.put("status", 0);
resultMap.put("fileName", newRealFileName);
}
} catch (Exception e) {
resultMap.put("status", 1);
resultMap.put("message", "图片上传出错");
logger.info("***** 图片上传出错 *****");
System.out.println(e);
} finally {
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
//必须设置字符编码,否则返回json会乱码
response.setContentType("text/html;charset=UTF-8");
out.write(JSONSerializer.toJSON(resultMap).toString());
out.flush();
out.close();
}
}
}
如果觉得《jquery的ajaxSubmit()异步上传图片并保存表单数据演示代码【jquery】》对你有帮助,请点赞、收藏,并留下你的观点哦!