1. html:

   

 1  <!-- 图片文本框 -->
 2     <input type="file" class="form-control" id="file" name="file" onchange="javascript:preview(this)">
 3     <!-- 这个是在上传之前回显图片图片展示 -->
 4     <div id="preview">
 5       <!--这个是为了将页面返回的图片展示出来的.默认隐藏-->
 6       <img style="width: 100px; height: 100px;display:none" id="imgHidden" />
 7     </div>
 8 
 9     <!-- 提交...这里pageIndex和pageSize可传可不传,主要取决于提交之后是否需要回到当前页面. -->
10     <button type="submit" onclick="javascript:submitForm()" class="btn btn-primary">提交</button>

 

   2. js:

    

function submitForm() {
                  var formData = new FormData(); //将需要提交的参数封装起来
                  formData.append("id", uuid);
                  var zswb = $("#file").val(); //获取file中的内容,看是否有值
                  if (zswb == '' || zswb.length < 1) { //没有file提交的时候走的接口
                    $.ajax({
                      url : '/editMovieWithoutFile',
                      type : 'post',
                      data : formData,
                      processData : false,
                      contentType : false,
                      success : function(value) {
                        var result = JSON.parse(value);
                        if (result == 'true') {
                          window.location.href = "/index?pageIndex=" + pageIndex+ "&pageSize=" + pageSize;
                        } else {
                          Lobibox.alert('error', {msg : "媒资信息更新失败!!!"});
                        }
                      }
                    });
                  } else { //有file提交的时候走的接口
                    formData.append("face", $("#file")[0].files[0]);
                    $.ajax({
                      url : '/bc/uploadFace',
                      type : 'POST',
                      data : formData,
                      processData : false,
                      contentType : false,
                      success : function(value) {
                        location.reload();
                      var result = JSON.parse(value);
                    if (result == 'true') {
                      window.location.href = "/index?pageIndex=" + pageIndex+ "&pageSize=" + pageSize;
                    } else {
                      Lobibox.alert('error', {msg : "媒资信息更新失败!!!"});
                    }
                   }
                  });
                 }
                }
                //图片回显:
                function preview(file) {
                  $("#imgHidden").css("display", "none");
                  var prevDiv = document.getElementById('preview');
                  if (file.files && file.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function(evt) {
                      prevDiv.innerHTML = '<img style="width: 100px;height: 100px;" src="' + evt.target.result + '" />';
                    }
                    reader.readAsDataURL(file.files[0]);
                  } else {
                    prevDiv.innerHTML = '<div class="img" style="width: 100px;height:100px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + 
                file.value + '\'"></div>';
                  }
                }

  3. web

    

 1 @RequestMapping(value = "/uploadFace", method = RequestMethod.POST)
 2     @ResponseBody
 3     public JSONObject uploadFace(@RequestParam("face") MultipartFile image, @RequestParam("id") Integer uid)
 4             {
 5         String contentType = image.getContentType();
 6         String[] spStrings =  contentType.split("/");
 7         JSONObject rt = new JSONObject();
 8         Integer code = 10001;
 9         String message = "fail";
10         if (spStrings.length > 1&&(spStrings[1].equals("jpeg")||spStrings[1].equals("png")||spStrings[1].equals("image")||spStrings[1].equals("img"))) {
11             Page<Client> findByUserId = clientSErvice.findByUserId(uid.toString(), null);
12             Client findOne = clientSErvice.findOne(uid);
13             if (findOne != null) {
14                 if (findOne.getPhoto() != null) {
15                     new File(face_lib + findOne.getPhoto()).delete();
16                 }
17                 String fileName = face_lib + findOne.getUsername()+"."+spStrings[1];
18                 FileOutputStream fos = null;
19                 try {
20                     fos = new FileOutputStream(new File(fileName));
21                 } catch (FileNotFoundException e) {
22                     // TODO Auto-generated catch block
23                     e.printStackTrace();
24                 }
25                 try {
26                     fos.write(image.getBytes());
27                 } catch (IOException e) {
28                     // TODO Auto-generated catch block
29                     e.printStackTrace();
30                 }
31                 try {
32                     fos.flush();
33                 } catch (IOException e) {
34                     // TODO Auto-generated catch block
35                     e.printStackTrace();
36                 }
37                 try {
38                     fos.close();
39                 } catch (IOException e) {
40                     // TODO Auto-generated catch block
41                     e.printStackTrace();
42                 }
43                 String username = findOne.getUsername();
44                 findOne.setPhoto(username+"."+spStrings[1]);
45                 clientSErvice.save(findOne);
46                 code = 10000;
47                 message="success";
48                 return rt;
49             }
50         }
51         return rt;
52     }

  Http content-type: https://tool.oschina.net/commons

posted on 2021-03-25 18:10  qiao1127  阅读(761)  评论(0)    收藏  举报