最近搜索

异步上传 2个input 上传 文件 如何弄 如何做

浏览:592
管理员 2020-02-19 10:41


function uploadFile2(file){
    $.ajaxFileUpload({
        url : '/admin/wx/config/add_imageUrl2', //用于文件上传的服务器端请求地址  
        secureuri : false, //一般设置为false
        fileElementId : 'file2', //文件上传空间的id属性  <input type="file" id="file" name="file" />  
        type : 'post',  
        dataType : 'text', //返回值类型 一般设置为json  
        success : function(result)//服务器成功响应处理函数  
        {
        	var result = $.parseJSON(result.replace(/<.*?>/ig,""));
            if(result.success){
            	console.log(result.path) ;
            }
        },
        error : function(result)//服务器响应失败处理函数  
        {  
        }
    });  
    return false;
}


<div style=" flex:1;-webkit-flex:1;"><input style="border: 0;" type="file" name="file2" id="file2" onchange ="uploadFile2(this)" class="layui-input" /></div>


后台代码和第一个是一样的。

只是改下接受的文件名子。

/**
	 * 
	 * /admin/wx/config/add_imageUrl2
	 */
	@ResponseBody
	@RequestMapping("/add_imageUrl2")
	public JSONObject add_imageUrl2(@RequestParam("file2") MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws Exception {
	        JSONObject result = new JSONObject();
	        if(!file.isEmpty()){
	            String webPath=request.getServletContext().getRealPath("");
	            System.out.println(webPath);
	            String filePath= "/static/upload_image/luck/"+DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
	            //把文件名子换成(时间搓.png)
	            // String imageName="houtai_logo."+file.getOriginalFilename().split("\\.")[1];
	            //检测   文件夹有没有创建 
	            FileUtil.makeDirs(webPath+filePath);
	            String imageName=DateUtil.formatDate(new Date(), "yyyyMMddHHmmss")+".jpg";
	            file.transferTo(new File(webPath+filePath+imageName));
	            result.put("success", true);
	            result.put("path", filePath+imageName);
	        }
	        return result;
	}



联系站长

站长微信:xiaomao0055

站长QQ:14496453