easyUpload.js插件文件上传

@RequestMapping(value = “/chse/file/upload”, method = RequestMethod.POST)
@ResponseBody
public JSONObject uploadFilesFromHtml(@RequestParam(“file”) MultipartFile[] files, HttpServletRequest request, ModelMap model ) throws IOException {
JSONObject jsonObject=new JSONObject();
String returnStr=”“;
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator iter = multipartRequest.getFileNames();
String fileName=”“;
String attachmentIds=”“;
while(iter.hasNext()){
MultipartFile file = multipartRequest.getFile(iter.next());
fileName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
long size = file.getSize();
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
StorePath storePath = storageClient.uploadFile(inputStream,size,extension ,null);
String group = storePath.getGroup();
String path = storePath.getPath();

        ResourceBundle bundle = ResourceBundle.getBundle("application");//读取application.properties文件,不加.properties后缀,不加路径名
        String url=bundle.getString("fastdfs.url");//地址
        String port=bundle.getString("fastdfs.port");//端口

        returnStr=url+":"+port+"/"+group+"/"+path;

/* String path = request.getSession().getServletContext().getRealPath(“/upload”);

// String[] split = fileName.split(“\.”);
// fileName = String.valueOf(System.currentTimeMillis()) +split[0]+ “.” + split[1];
File targetFile = new File(path, fileName);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}

        //保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        model.addAttribute("fileUrl",returnStr);
     /*   model.addAttribute("fileUrl",request.getContextPath()+"/static/upload/"+fileName);*/
     /*   returnStr=request.getContextPath()+"/static/upload/"+fileName;*/
        /*      sb.append(request.getContextPath()+"/images/upload/"+fileName+"|");*/
    }

    if(!returnStr.equals("")){
        //附件表传入记录
        AttachmentBean attachmentBean=new AttachmentBean();
        attachmentBean.setId(UUID.randomUUID().toString());
        attachmentBean.setFileName(fileName);
        attachmentBean.setUrl(returnStr);
        attachmentService.insert(attachmentBean);
        attachmentIds=attachmentIds+attachmentBean.getId()+";";
        jsonObject.put("easyFileIndex","");
        jsonObject.put("code",200);
        jsonObject.put("data",attachmentIds.substring(0,attachmentIds.length()-1));
    }else{
        jsonObject.put("easyFileIndex","");
        jsonObject.put("code",300);
    }
    return jsonObject;
}






/**
 *附件上传
 */
$('#easyContainerEditor').easyUpload({
    allowFileTypes: '*.docx;*.doc;*.pdf;*.txt;*.xlsx;*.rar;*.xls;*.ppt;*.pptx',//允许上传文件类型,格式';*.doc;*.pdf'
    allowFileSize: 100000,//允许上传文件大小(KB)
    selectText: '选择文件',//选择文件按钮文案
    multi: true,//是否允许多文件上传
    multiNum: 10,//多文件上传时允许的文件数
    showNote: true,//是否展示文件上传说明
    note: '提示:最多上传10个文件,支持格式为doc、pdf、docx、txt、xlsx、xls、rar、pptx、ppt',//文件上传说明
    showPreview: true,//是否显示文件预览
    url: 'chse/file/upload',//上传文件地址
    fileName: 'file',//文件filename配置参数
    formParam: {
        token: $.cookie('token_cookie')//不需要验证token时可以去掉
    },//文件filename以外的配置参数,格式:{key1:value1,key2:value2}
    timeout: 30000,//请求超时时间
    okCode: 200,//与后端返回数据code值一致时执行成功回调,不配置默认200
    successFunc: function(res) {
        console.log('成功回调', res);
        var fileIds="";
        for (var i=0;i<res.success.length;i++){
            fileIds=fileIds+res.success[i].data+";";
        }
        $("#attachmentEditor").val(fileIds.substring(0,fileIds.length-1));
    },//上传成功回调函数
    errorFunc: function(res) {
        console.log('失败回调', res);
    },//上传失败回调函数
    deleteFunc: function(res) {
        console.log('删除回调', res);
        var fileIds="";
        for (var i=0;i<res.success.length;i++){
            fileIds=fileIds+res.success[i].data+";";
        }
        $("#attachmentEditor").val(fileIds.substring(0,fileIds.length-1));
    }//删除文件回调函数
});

来源:https://download.csdn.net/download/qq_38270645/10624736 全部

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页