• 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);
    
            }
    
    
        }
  • 相关阅读:
    CQOI2009中位数图
    CQOI2011分金币&HAOI2008糖果传递
    SCOI2010游戏
    JSOI2007建筑抢修
    HNOI2008明明的烦恼
    SCOI2009生日快乐
    (22/24) webpack实战技巧:静态资源集中输出
    (22/24) webpack实战技巧:静态资源集中输出
    [mysql]linux mysql 读写分离
    [mysql]linux mysql 读写分离
  • 原文地址:https://www.cnblogs.com/kinome/p/9959794.html
Copyright © 2020-2023  润新知