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 ...
随机推荐
- SCLAlertView-Swift
SCLAlertView-Swift https://github.com/vikmeup/SCLAlertView-Swift Animated Alert View written in Sw ...
- [UI] Elastic Stack & scrollReveal.js
Elastic Stack & scrollReveal.js Elastic Stack http://freebiesbug.com/code-stuff/elastistack-js-d ...
- WinPE ISO制作
1.安装ADK,然后导出winPE镜像文件和启动文件: 打开 部署和映像工具环境,cd "Windows Preinstallation Environment",运行 copyp ...
- eclipse中 项目-->属性-->为什么没有deployment assembly 选项
原因: 因为当前的maven工程不是web工程,需要转换成web工程. 解决方法: 右击工程属性,找到Project Facets,选择Dynamic Web Module,2.5 点击apply.这 ...
- 实现body背景拉伸自适应 兼容chrome ie7,8,9.ie6未测试
html, body {/*此部分支持chrome,应该也支持firefox*/ background: rgb(246,248,249); background: url('/styles/imag ...
- React Native 基础报错及解决方案记录
刚开始上手RN,碰到很多坑,记录一下.碰到问题多去看看github上面的issue! 启动命令react-native run-ios报错 1.:xcrun: error: unable to fin ...
- 学习Android之SimpleAdapter显示网络图片
效果图: 此程序基本的知识点是:SimpleAdapter本身是不支持网络图片的, 假设在Map.put(a,b)中 b为一个Bitmap,程序不会报红色字体,而是在控制台输出绿色的字体,例如以下 0 ...
- memcached迁移方案——记一次memcached session服务的迁移
背景: (1)由于机房调整,需要迁移memcached: (2)需要在短期内迁移完成(一周以内): (3)该memcached 保存了用户的登录数据,非常重要,一旦出问题将导致大量的用户被踢出: (4 ...
- 看到了一个pipeline例子,
pipeline { agent any options { timestamps() } parameters { string(name: 'GIT_BRANCH', defaultValue: ...
- python 模拟126邮箱登陆
#coding=utf-8from selenium import webdriverimport time mydriver=webdriver.Firefox()mydriver.get(&quo ...