JavaWeb项目中使用ajax上传文件
1.jsp
$("#cxsc").click(function(){
var bankId = $("#bankId").val();
var formdata = new FormData();
formdata.append('logo', $('#btnFile').get(0).files[0]);
formdata.append('bankId', bankId);
$.ajax({
type: 'POST',
url: './uploadLogo',
contentType : false,
data : formdata,
processData : false,
dataType: "json",
success: function (data) {
$("#logoImg").attr('src','${_b}/upload/banklogo/'+data.msg);
},
error : function(data) {
alert('上传失败!');
}
});
<#if formData?exists>
<#if (formData.logoImg??)>
<img src="${_b}/upload/banklogo/${formData.logoImg}" style="width:120px;height:120px;margin-bottom:5px;" id="logoImg"/>
<br/>
<input type="file" name="logo" id="btnFile" style="border:none;display:inline">
<button type="button" id="cxsc" style="display:inline">上传</button>
<#else>
<input type="file" name="logo" style="border:none">
</#if>
<#else>
<input type="file" name="logo" style="border:none">
</#if>
2.controller
@RequestMapping(value = "/uploadLogo", method = {RequestMethod.POST})
public void uploadLogo(
@RequestParam(value = "bankId", required = true) String bankId,
@RequestParam("logo") MultipartFile logo,
HttpServletRequest request, HttpServletResponse response, ModelMap model) {
Json json = new Json();
BankManage bankManage = bankManageService.getById(bankId);
if (bankManage != null) {
try {
if (!logo.isEmpty()) {
String relativePath = "/upload/banklogo";
// 旧图片路径
String absolutePath = request.getSession().getServletContext().getRealPath(relativePath)+"\\"+bankManage.getLogoImg();
File oldfile = new File(absolutePath);
if (oldfile.exists()) {
oldfile.delete(); // 删除旧图片
}
String newPath = request.getSession().getServletContext().getRealPath(relativePath)+"\\"+logo.getOriginalFilename();
File newFile = new File(newPath);
logo.transferTo(newFile);
bankManage.setLogoImg(logo.getOriginalFilename());
bankManageService.update(bankManage);
json.setMsg(logo.getOriginalFilename());
writeJson(request, response, json);
}else {
json.setMsg("上传失败!");
writeJson(request, response, json);
}
}catch (Exception e) {
e.printStackTrace();
logger.error(e);
}
}
}
JavaWeb项目中使用ajax上传文件的更多相关文章
- javaWeb中使用ajax上传文件
javaWeb上传图片 上传文件所必要的两个jar包:commons-fileupload.jar.commons-io.jar. jar包下载:github路径 核心代码: String withP ...
- ssm项目中常用的上传文件
在项目中,上传文件一般是必不可少的,所以今天学到新的上传方式,就干脆将学习过的上传方式记录一下 一.表单直接上传图片 表单头要设置 <form action="" metho ...
- ajax上传文件显示进度
下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...
- django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件
一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- springMVC+jsp+ajax上传文件
工作中遇到的小问题,做个笔记 实现springMVC + jsp + ajax 上传文件 HTML <body> <form id="myform" method ...
- Ajax上传文件进度条显示
要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小 html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大 小/总大小,计算上传的百分比,然后用这个百分比控制div框的显 ...
- 伪ajax上传文件
伪ajax上传文件 最近在折腾伪ajax异步上传文件. 网上搜索了一下,发现大部分方法的input file控件都局限于form中,如果是在form外的呢? 必须动态生成一个临时form和临时if ...
- flask jQuery ajax 上传文件
1.html 代码 <div> <form id="uploadForm" enctype="multipart/form-data" > ...
随机推荐
- bzoj1150 [CTSC2007]数据备份
Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中 ...
- 前端高质量知识(四)-JS详细图解作用域链与闭包
攻克闭包难题 初学JavaScript的时候,我在学习闭包上,走了很多弯路.而这次重新回过头来对基础知识进行梳理,要讲清楚闭包,也是一个非常大的挑战. 闭包有多重要?如果你是初入前端的朋友,我没有办法 ...
- python_3_coding
# -*- coding:utf-8 -*- 也可以换成下划线 用于声明文件编码,python3本身就是utf-8类型,不用声明 name="你好,世界"#utf-8格式能显示汉字 ...
- vue input框设置值 一般对象都是通过打点形式取值
- python中的for循环如何控制步长
for i in range(开始/左边界, 结束/右边界, 步长): print i 例如 for i in range(1, 10, 2): print i 等价于 for (i=1;i<= ...
- 居中未知元素(翻译https://css-tricks.com/centering-in-the-unknown/)
在web开发中,当你遇到居中元素时,知道越多关于元素本身和父级元素的信息,居中做起来就很轻松.但是,当遇到你一点都不知道的元素该怎么办? It's still kinda doable. 不会很难:已 ...
- Servlet学习笔记05——什么是jsp?
1. jsp (java server page) (1)jsp是什么? sun公司制订的一种服务器端动态页面技术规范. 注: 因为虽然使用servlet也可以生成动态页面, 但是过于繁琐(需要使用o ...
- PHP中json_encode后,在json字符串中依然显示中文的解决方案
<?php header("Content-Type:text/html;charset=utf-8;"); $arr = array ('Version_code'=> ...
- [Wolfgang Mauerer] 深入linux 内核架构 第一章 概述
作为Linux开发爱好者,从事linux 开发有两年多时间.做过bsp移植,熟悉u-boot代码执行流程:看过几遍<linux 设备驱动程序开发>,分析过kernel启动流程,写过驱动,分 ...
- python json.dumps raise TypeError(repr(o) + " is not JSON serializable") TypeError: 0 is not JSON serializable
出错如题. 这个问题有可能是因为python的json.dumps没法识别dump内容里的某些数据类型导致的.我的问题是因为dict中含有numpy.int64,numpy.float等类型导致的,需 ...