使用JavaScript预览Spring MVC中的图像

时间:2016-05-16 20:13:14

标签: javascript java html spring spring-mvc

我正在编写一个Spring MVC应用程序,用于将图像上传到服务器。我的Java部分工作正常,但我需要JavaScript,我不是很熟悉它,很抱歉,如果我问一些明显的东西。

当我在我的视图中仅使用具有以下html表单的控制器时,一切正常:

<form method="POST" action="upload" enctype="multipart/form-data">
        <input type="file" name="file" multiple><br/>
        <input type="submit" value="Upload">
</form>

我只是在控制器中使用@RequestMapping拦截它并获取我的文件。

但我需要在上传之前预览我的照片。我发现了以下代码,它也很有效:

<html>
<head>
<style type="text/css">.thumb-image{float:left;width:100px;position:relative;padding:5px;}.selectedItem{border:2px solid blue;}</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="wrapper" style="margin-top: 20px;">
<p><b>For deleting thumb-image: click on image to select and press delete button.</b> </p>
<input id="fileUpload" multiple="multiple" type="file"/> 
<button id="btnDelete">Delete</button>
<div id="image-holder">
</div>
<br/><br/>

</div>

<script>
$(document).ready(function() {

$("#image-holder").on('click','.thumb-image',function(){
   $(this).toggleClass("selectedItem");
});

$("#btnDelete").on("click",function(){
$(".selectedItem").remove();
});

        $("#fileUpload").on('change', function() {
          //Get count of selected files
          var countFiles = $(this)[0].files.length;
          var imgPath = $(this)[0].value;
          var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
          var image_holder = $("#image-holder");
          image_holder.empty();
          if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
            if (typeof(FileReader) != "undefined") {
              //loop for each file selected for uploaded.
              for (var i = 0; i < countFiles; i++) 
              {
                var reader = new FileReader();
                reader.onload = function(e) {
                  $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                  }).appendTo(image_holder);
                }
                image_holder.show();
                reader.readAsDataURL($(this)[0].files[i]);
              }
            } else {
              alert("This browser does not support FileReader.");
            }
          } else {
            alert("Pls select only images");
          }
        });
      });
</script>
</body>
</html>

但我现在不知道如何将它传递给我的控制器。我应该在哪里放置action="upload"或其他东西才能使其正常工作?此处没有表单,也没有提交按钮。谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个;)

<html>
<head>
<style type="text/css">.thumb-image{float:left;width:100px;position:relative;padding:5px;}.selectedItem{border:2px solid blue;}</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="wrapper" style="margin-top: 20px;">
<p><b>For deleting thumb-image: click on image to select and press delete button.</b> </p>
<form method="POST" action="upload" enctype="multipart/form-data">
<input id="fileUpload" multiple="multiple" type="file"/> 
<input type="submit" value="Upload">
<button id="btnDelete">Delete</button>
</form>
<div id="image-holder">
</div>
<br/><br/>

</div>

<script>
$(document).ready(function() {

$("#image-holder").on('click','.thumb-image',function(){
   $(this).toggleClass("selectedItem");
});

$("#btnDelete").on("click",function(){
$(".selectedItem").remove();
});

        $("#fileUpload").on('change', function() {
          //Get count of selected files
          var countFiles = $(this)[0].files.length;
          var imgPath = $(this)[0].value;
          var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
          var image_holder = $("#image-holder");
          image_holder.empty();
          if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
            if (typeof(FileReader) != "undefined") {
              //loop for each file selected for uploaded.
              for (var i = 0; i < countFiles; i++) 
              {
                var reader = new FileReader();
                reader.onload = function(e) {
                  $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                  }).appendTo(image_holder);
                }
                image_holder.show();
                reader.readAsDataURL($(this)[0].files[i]);
              }
            } else {
              alert("This browser does not support FileReader.");
            }
          } else {
            alert("Pls select only images");
          }
        });
      });
</script>
</body>
</html>