天天看點

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 全部