DropZone图片上传控件的使用

前台代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
  <script type="text/javascript" src="Dropzone/dropzone.min.js"></script>
  <link rel="stylesheet" type="text/css" href="Dropzone/css/basic.css"><link>
  <link rel="stylesheet" type="text/css" href="Dropzone/css/dropzone.css"><link>

  <script>
  
   Dropzone.options.dropzoneForm = {
           //添加上传取消和删除预览图片的链接,默认不添加
        addRemoveLinks: true,
        init : function() {     
            this.on("success", function(file, res){});
            //删除图片的事件,当上传的图片为空时,使上传按钮不可用状态
            this.on("removedfile", function () {
                if (this.getAcceptedFiles().length === 0) {
                    $("#dropzoneForm").attr("disabled", true);
                }
            });
        }
    };
  
  </script>

 <body>
  
<form action="<%= request.getContextPath()%>/vehicleBasic/upload.do" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneForm">
</form>

 </body>
</html>

后台代码:

    @RequestMapping("upload")  
    @ResponseBody  
    public String upload(@RequestParam(value = "file", required = false)  MultipartFile file,
            HttpServletRequest request, HttpServletResponse response) throws IOException{  
        response.setHeader("Access-Control-Allow-Origin","*");
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
         String res = sdf.format(new Date());
         // uploads文件夹位置
         //String rootPath = request.getSession().getServletContext().getRealPath("vehFile");
         String rootPath = "ehFile/";         
         // 原始名称
         String originalFileName = file.getOriginalFilename();
         // 新文件名
         String newFileName = res + originalFileName.substring(originalFileName.lastIndexOf("."));
         // 新文件
         File newFile = new File(rootPath +File.separator + newFileName);
         // 判断目标文件所在目录是否存在
         if( !newFile.getParentFile().exists()) {
             // 如果目标文件所在的目录不存在,则创建父目录
             newFile.getParentFile().mkdirs();
         }
         System.out.println(newFile);
         System.out.println(originalFileName);
         // 将内存中的数据写入磁盘
         file.transferTo(newFile);
         // 完整的url
         String fileUrl =rootPath + newFileName;    
         return  fileUrl;
    } 

DropZone资源路径:

 http://download.csdn.net/download/s0009527/10198590

转载于:https://www.cnblogs.com/YLQBL/p/8258822.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值