jquery ajax 上传文件和传递参数到一个接口的实现方法
参考:https://blog.csdn.net/qq_15674631/article/details/81095284
参考:https://www.jianshu.com/p/46e6e03a0d53 (包含了上传多个文件的方法)
总结: ajax 如果想同时发送文件和参数,那就必须用 formData 封装,如果想接收单个参数,直接key:value的形式写到data:{}里或者用formData append都可以,后端springmvc接收直接用 String xxx , int xxx ,这样接收就行了,不过参数如果多了最好用bean封装
<input type="file" id="newTemplateFileId" >
var formData = new FormData();
formData.append('file', $('#newTemplateFileId')[0].files[0]);
formData.append('templatename', $('#newTemplateNameId').val());
formData.append('systype', templateName);
formData.append('templatetype', templatetype);
formData.append('templatejson', templateJsonContent);
$.ajax({
// url:'/RM/views/test/statusJson.json',
url: '/RM/controller/json/ConfigTemplateController/saveMobanContentsAndUploadUserCustomScripts',
type: 'POST',
cache: false,
processData: false,
contentType: false,
data: formData,
success: function (response) {
if (response.status == "1") {
// $('#nextStepBtnId').attr('data-toggle','modal').attr('data-target','#myModal');
alert('保存成功')
}else if (response.status == "0") {
alert(response.msg);
}
}
})
@ResponseBody
@RequestMapping(value = "controller/json/ConfigTemplateController/saveMobanContentsAndUploadUserCustomScripts", method = RequestMethod.POST)
public BaseResult saveMobanContentsAndUploadUserCustomScripts(@Valid ConfigTemplateCustom configTemplateCustom, BindingResult result, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException { if (result.hasErrors()) {
Map<String, Object> map = new HashMap<>();
List<FieldError> fieldErrors = result.getFieldErrors();
for (FieldError fieldError :
fieldErrors) {
map.put(fieldError.getField(), fieldError.getDefaultMessage());
}
return ResultUtil.error(303, "参数异常").add("errorFields", map);
} else { if (configTemplateCustom.getTemplatename() == null || configTemplateCustom.getTemplatename()=="" || configTemplateCustom.getTemplatename().isEmpty()) {
return ResultUtil.error(400, "请填写模板名");
} if (file == null || file.isEmpty()) {
return ResultUtil.error(400, "文件为空,请上传文件后再保存模板");
} // 文件类型分拣
int fileNameLength = file.getOriginalFilename().length();
String beHandFileName = new String(file.getOriginalFilename());
String beHandSuffixName = null;
String beHandedFileNameVbs = beHandFileName.substring(fileNameLength - 4, fileNameLength);
String beHandedFileNameSh = beHandFileName.substring(fileNameLength - 3, fileNameLength);
String beHandedFileNamePl = beHandFileName.substring(fileNameLength - 3, fileNameLength); if (!".sh".equals(beHandedFileNameSh) && !".pl".equals(beHandedFileNamePl)) {
return ResultUtil.error(400, "上传的脚本类型不匹配,当前只支持类unix系列的远程扫描,请上传后缀名为 .sh .pl 的脚本文件");
} if (configTemplateCustomService.checkTemplateName(configTemplateCustom)==false){
return ResultUtil.error(400, "模板名已被使用,请填写新的模板名");
}
ConfigTemplateCustom savedTemplateCustom = configTemplateCustomService.saveAfterFetchId(configTemplateCustom); // if (".vbs".equals(beHandedFileNameVbs)) {
// beHandSuffixName = ".vbs";
// } if (".sh".equals(beHandedFileNameSh)) {
beHandSuffixName = ".sh";
}
if (".pl".equals(beHandedFileNamePl)) {
beHandSuffixName = ".pl";
} File templateDir = new File("C:\\RM-ROOT\\upload\\userCustomScripts\\" + configTemplateCustom.getTemplatename());
templateDir.mkdir(); String path = "C:\\RM-ROOT\\upload\\userCustomScripts\\" + configTemplateCustom.getTemplatename();
String uploadedFileName = configTemplateCustom.getTemplatename() + beHandSuffixName; FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,
uploadedFileName)); return ResultUtil.success().add("savedTemplateCustom", savedTemplateCustom); } }
jquery ajax 上传文件和传递参数到一个接口的实现方法的更多相关文章
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- jQuery ajax上传文件实例
jQuery ajax上传文件实例 <form id="form" enctype="multipart/form-data"><input ...
- flask jQuery ajax 上传文件
1.html 代码 <div> <form id="uploadForm" enctype="multipart/form-data" > ...
- jquery ajax 上传文件
html:<!-- /.tab-pane --> <div class="tab-pane" id="head_portrait"> & ...
- ASP.NET Jquery+ajax上传文件(带进度条)
效果图 支持ie6+,chrome,ie6中文文件名会显示乱码. 上传时候会显示进度条. 需要jquery.uploadify.js插件,稍后会给出下载 前台代码 <%@ Page Langua ...
- jQuery Ajax 上传文件改进
如果用户取消上传后 背景 提示自动消失了.... 修正Bug.... 同时也更新了不同上传类型的提示字体大小... 2017-05-26 增加了鼠标释放提示 先看之前的效果: 再看现在的效果: 升级 ...
- jQuery Ajax 上传文件夹及文件
我们先来看一下文件夹结构 这是上传处理的: 看一下系统日志: 升级 HTML5文件实现拖拽上传提示效果改进(支持三种状态提示) 拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件 ...
- asp.net 中使用JQuery Ajax 上传文件
首先创建一个网页,网页中添加如下代码. <h3>Upload File using Jquery AJAX in Asp.net</h3> <table> < ...
- jQuery Ajax上传文件
JS代码: //保存 function btnAdd() { var formData = new FormData($("#frm")[0]); $.ajax({ url: &q ...
随机推荐
- [翻译] RSKImageCropper
RSKImageCropper https://github.com/ruslanskorb/RSKImageCropper An image cropper for iOS like in the ...
- pandas模块安装问题笔记
1. # pip install pandas 引用 pandas 时,没有模块 ,进行模块安装,出现一推英文提示 结果 Collecting pandas Could not fetch URL ...
- 铁乐学python_Day42_线程池
铁乐学python_Day42_线程池 concurrent.futures 异步调用模块 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor: ...
- Flex布局及其应用
什么是弹性盒子? 弹性盒子是 CSS3 的一种新的布局模式.相对于传统的依赖于display+position+float的布局方式,弹性盒子更加以有效的方式来对一个容器中的子元素进行排列.对齐和分配 ...
- 【原创】uwsgi中多进程+多线程原因以及串行化accept() - thunder_lock说明
如有不对,请详细指正. 最近再研究uwsgi如何部署python app,看uwsgi的文档,里面有太多的参数,但每个参数的解释太苍白,作为菜鸟的我实在是不懂.想搞清楚uwsgi的工作原因以及里面的一 ...
- Spring-IOC 在非 web 环境下优雅关闭容器
当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...
- Jython的应用
今天本文围绕主要内容是jython是什么.安装.简单实用. 另外说说我为什么研究jython,研究它是有一个目的的,目的是将python代码转化为jar包以供安卓方面那边人脸识别,虽说目前人脸识别像阿 ...
- 多线程之ThreadLocal(转)
相信读者在网上也看了很多关于ThreadLocal的资料,很多博客都这样说:ThreadLocal为解决多线程程序的并发问题提供了一种新的思路:ThreadLocal的目的是为了解决多线程访问资源时的 ...
- App界面设计利器Sketch 精选案例合集
第1章 课程介绍主要介绍课程的安排及你将学到哪些使用技巧 1-1 课程介绍第2章 sketch 实例及相关工具本节课你将学到钢笔工具的使用.渐变及填充.投影的使用,体会并观察实际中物品的光影与材质:模 ...
- Spring源码分析(十七)循环依赖
本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 实例化bean是一个非常复杂的过程,而其中比较难以理解的就是对循环依赖的解决, ...