<!-- jquery file upload相关js -->

<script src="/js/jquery-file-upload/js/jquery.ui.widget.js"></script>
<script src="/js/jquery-file-upload/js/jquery.iframe-transport.js"></script>
<script src="/js/jquery-file-upload/js/jquery.fileupload.js"></script>
<script src="/js/jquery-file-upload/js/jquery.fileupload-process.js"></script>
<script src="/js/jquery-file-upload/js/jquery.fileupload-validate.js"></script>
<link rel="stylesheet" href="/js/jquery-file-upload/css/jquery.fileupload.css">
<link rel="stylesheet" href="/js/jquery-file-upload/css/jquery.fileupload-ui.css">

$('#upload_cover').fileupload({
  url: '/img/upload',
  acceptFileTypes: /(\.|\/)(png)$/i,//文件后缀控制
  maxNumberOfFiles: 1,//最大上传文件数目
  maxFileSize: 50000,
  dataType: 'json',//期望从服务器得到json类型的返回数据
  messages : {//文件错误信息
  acceptFileTypes : '文件类型不匹配',
  maxFileSize : '文件过大',
  },processfail: function (e, data) {
    var currentFile = data.files[data.index];
    if (data.files.error && currentFile.error) {
      console.log(currentFile.error);
      $('#error').text(currentFile.error);
    }
  },
  done: function (e, data) {
    $('#imgUrl').val(data.result.fileUrl);
    $('#error').text("");
  },
  fail: function (e, data) {
    $('#error').text("上传失败!"+data.files.error);
  }
});

<!--html代码,样式使用bootstrap-->

<div class="form-group ">

  <label class="control-label col-lg-2"></label>

  <div class="col-sm-5">

    <span class="btn btn-success fileinput-button">选择<input type="file" id="upload_cover" /></span>

     <font color="red" id="error"></font><br/>
  </div>
</div>
<div class="form-group ">
  <label class="control-label col-lg-2">imgUrl<font color="red">*</font></label>
  <div class="col-lg-10">
    <input type="text" name="imgUrl" id="imgUrl" class="form-control" placeholder="请选择" value="" readonly="readonly"/>
  </div>
</div>

<!--服务端代码-->

@RequestMapping(value = "/img/upload", method = { RequestMethod.POST })
public void toUpload(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
  // 创建一个通用的多部分解析器
  CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
  // 新建目录
  String savePath = Constans.FILE_UPLOAD_PATH + Constans.IMG_FOLDER;
  File directory = new File(savePath);
  if (!directory.exists()) {
    directory.mkdirs();
  }
  try {
    // 判断 request 是否有文件上传,即多部分请求
    if (multipartResolver.isMultipart(request)) {
      // 转换成多部分request
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
      // 取得request中的所有文件名
      Iterator<String> iter = multiRequest.getFileNames();
      while (iter.hasNext()) {
        // 取得上传文件
        MultipartFile file = multiRequest.getFile(iter.next());
        if (file != null) {
          // 取得当前上传文件的文件名称
          String myFileName = file.getOriginalFilename();
          if (myFileName.trim() != "") {
            // 定义上传路径
            String tarpath = savePath + myFileName;
            File localFile = new File(tarpath);
            file.transferTo(localFile);
            model.addAttribute("fileUrl", tarpath);
            model.addAttribute("fileName", myFileName);
          }
        }

      }

    }
    logger.info(">>> upload complete");
  } catch (Exception e) {
    e.printStackTrace();
    logger.info(">>> upload error");
  } finally {
    response.setContentType("application/json; charset=utf-8");
    response.setCharacterEncoding("utf-8");
    PrintWriter pw = response.getWriter();
    pw.print(new Gson().toJson(model));
    pw.flush();
    pw.close();
  }

}

jquery file upload使用的更多相关文章

  1. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  2. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

  3. 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮

    需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...

  4. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  5. jQuery File Upload跨域上传

    最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...

  6. jquery ajax发送delete(use in jquery file upload delete file)

    环境: jQuery file upload HTML example code <div class="pic-preview"> <div class=&qu ...

  7. jquery file upload 后台收到的文件名中文乱码, filename中文乱码

    在jQuery File Upload.js文件里,在以下这个js中有个成员叫做 _initXHRData, 是一个function, 在这个function的最后部分有一个if-else分支,如下:

  8. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...

  9. jQuery File Upload 插件 php代码分析

    jquery file upload php代码分析首先进入构造方法 __construct() 再进入 initialize()因为我是post方式传的数据  在进入initialize()中的po ...

  10. jQuery File Upload blueimp with struts2 简单试用

    Official Site的话随便搜索就可以去了 另外新版PHP似乎都有问题  虽然图片都可以上传  但是response报错  我下载的是8.8.7木有问题   但是8.8.7版本结合修改main. ...

随机推荐

  1. HTML5新增的标签和属性归纳

    收集总结的HTML5的新特性,基本除了IE9以下都可以使用. HTML5语法 大部分延续了html的语法 不同之处:开头的 <!DOCTYPE html> <html lang=&q ...

  2. 一、C#简单读写

    using System.IO; static string configFileName = "config.json"; //不存在就直接新建文件夹 public static ...

  3. [tools]excel转lua的python实现

    time:2015/04/13 描述:需要将excel表格内容转成lua,并且作为工具使用,能够批量转换 步骤: (1)文章[1]已经做了大部分的内容,而且也已经能够使用了 (2)根据自己新的需求: ...

  4. centos 安装php扩展的两种方法

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   查看PHP版本: php -v 1 1 查看指定PHP版本: /usr/local/php/bin/php -v 1 1 ...

  5. 用setTimeout实现动态时钟的效果

    1.获取到系统时间 2.获取到当地时间字符串 3.开启延时器,每一秒刷新一次时间 <!DOCTYPE html> <html> <head> <meta ch ...

  6. github发布静态页面

    github发布静态页面:https://wangc1993.github.io/2019/01/07/2/

  7. codeforces 549F Yura and Developers(分治、启发式合并)

    codeforces 549F Yura and Developers 题意 给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 题解 分治,对于一个区间,找出最大值之后,分成两个区间. ...

  8. MySQL监控内容

    一.liunx操作系统层面1.整体cpu负载的%user最好不长期超过20%(若%user太高,有极大可能性是索引使用不当)2.整体cpu负载的%iowat最好不长期超过10%(确认I/O子系统是否有 ...

  9. Docker扩展.md

    逻辑卷 Volume 数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性: 数据卷可以在容器之间共享和重用 对数据卷的修改会立马生效 对数据卷的更新,不会影响镜像 数 ...

  10. python with原型

    @Python 的 with 语句详解   这篇文章主要介绍了Python 的 with 语句,本文详细讲解了with语句.with语句的历史.with语句的使用例子等,需要的朋友可以参考下   一. ...