• vue文件上传


    前端部分

        <input
          id="upload"
          style="display:none"
          type="file"
          @change="getFile($event)"
        />
    
        getFile (event) {
          let self = this
          console.log(event.target.files)
          if (event.target.files[0].name.indexOf(this.upFileType) > -1) {
            let file = event.target.files[0]
            let formData = new FormData()
            formData.append('project', this.upProject)
            formData.append('moduleId', this.upModuleId)
            formData.append('module', this.upModule)
            formData.append('fileInfo', file)
            formData.append('initFileType', this.upFileType)
            commData
              .uploadCon(formData)
              .then(data => {
                console.log(data)
                if (data.success) {
                  // 这边需要清空,不然重复文件不会继续上传
                  event.srcElement.value = ''
                  self.$message({
                    type: 'success',
                    message: data.msg
                  })
                  if (this.upProject === 'init') {
                    self.getInitProData(1, this.initProPageInfo.pageSize)
                  } else {
                    self.getOtherProData(1, this.otherProPageInfo.pageSize)
                  }
                } else {
                  self.$message({
                    type: 'error',
                    message: data.msg
                  })
                }
              })
              .catch(e => {
                self.$message.error('服务端出错')
                console.error(e)
              })
          } else {
            this.$message({
              type: 'warning',
              message: '上传文件和所选格式不一致!'
            })
          }
        }

    请求头部

    const remoteStartByFile = postparam => {
      return new Promise((resolve, reject) => {
        axios
          .post('/comm/uploadCon', postparam, {
            headers: { 'Content-Type': 'multipart/form-data' }
          })
          .then(response => {
            resolve(response)
          })
          .catch(e => {
            reject(e)
          })
      })
    }

    后台部分

        @RequestMapping(value = "/uploadCon")
        @ResponseBody
        public TResult<String> uploadCon(HttpServletRequest request,HttpServletResponse response,
                @RequestParam(value = "fileInfo", required = false) MultipartFile file,
                @RequestParam(value = "project", required = false) String project,
                @RequestParam(value = "module", required = false) String module,
                @RequestParam(value = "moduleId", required = false) String moduleId,
                @RequestParam(value = "initFileType", required = false) String initFileType) throws Exception {
            TResult<String> result = new TResult<String>();
            try {
                projectConfigService.uploadInitCon(file, project, module, moduleId, initFileType);
                result.setSuccess(true);
                result.setMsg("上传成功");
            } catch (Exception e) {
                logger.error(e.getMessage());
                result.setSuccess(false);
                result.setMsg("上传失败");
            }
            
            return result;
        }
        @Transactional
        public void uploadInitCon(MultipartFile file, String project,String module,
                String moduleId, String fileType) throws IOException {
            // 如果是初始化项目,那么将删除所有的
            if ("init".equals(project)) {
                ProjectConfig delconfig = new ProjectConfig();
                delconfig.setModule(module);
                delconfig.setFileType(fileType);
                iProjectConfigDao.delProjectConfig(delconfig);
            } else {
                ProjectConfig delconfig = new ProjectConfig();
                delconfig.setModuleId(moduleId);
                delconfig.setProject(project);
                delconfig.setFileType(fileType);
                iProjectConfigDao.delProjectConfig(delconfig);
            }
            
            logger.info("删除配置文件完毕,project: " + project + ",module:" + module + ",fileType:" + fileType);
            InputStream in = file.getInputStream();
            InputStreamReader read = new InputStreamReader(in, "utf-8");//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            int order = 1;
            while((lineTxt = bufferedReader.readLine()) != null){
                if (StringUtils.isBlank(lineTxt) || lineTxt.startsWith("#")) {
                    continue;
                }
                String arr[] = lineTxt.split("=");
                ProjectConfig config = new ProjectConfig();
                config.setProject(project);
                config.setModule(module);
                config.setModuleId(moduleId);
                config.setFileType(fileType);
                config.setParm(arr[0]);
                if (arr.length > 1 && StringUtils.isNotBlank(arr[1])) {
                    config.setParmValue(arr[1]);
                }
                config.setUnicode("0"); // 不需要unicode编码
                config.setAlterFlag("0"); // 客户化标识
                config.setOrderby(order++ + "");
                config.setId(order++ + "");
    //            先删除?
    //            ProjectConfig maxData = iProjectConfigDao.getMaxData(project, moduleId, fileType);
    //            config.setId(maxData.getId());
    //            config.setParmDesc(parmDesc);
                System.out.println(lineTxt);
                iProjectConfigDao.saveConfig(config);
            }
            in.close();
        }

    越努力越幸运~ 加油ヾ(◍°∇°◍)ノ゙
  • 相关阅读:
    使用 PyCharm 远程调试 Django 项目
    (坑集)Python环境配置
    字典的使用
    列表的使用
    字符串的魔法
    php 文件函数
    php 时间函数
    php xajax库基本知识
    php header函数
    c++注释
  • 原文地址:https://www.cnblogs.com/utomboy/p/15741347.html
Copyright © 2020-2023  润新知