2016-05-16 28 views
0

我正在編寫一個將圖像上傳到服務器的Spring MVC應用程序。我的Java部分工作正常,但我需要JavaScript,我不太熟悉它,所以很抱歉,如果我問一些明顯的問題。使用JavaScript預覽Spring MVC上傳圖像

當我只用控制器具有以下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"或其他什麼東西才能使它工作?這裏沒有表單和提交按鈕。謝謝!

+0

可以使用'

'包裹'<輸入的ID = 「文件上傳」 多重= 「多個」 類型= 「文件」/>'只像你所做的一樣。 – Mippy

回答

1

嘗試此)

<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>

+0

謝謝,它工作,@Mippy,但還有一個問題:當我選擇預覽並按下刪除按鈕,它只刪除預覽,但不會影響選定的文件列表,所以當我按上傳按鈕時,它會加載所有文件到服務器。你能幫我解決一下嗎? –

+0

@OlegShankovskyi,你可以做更多的Google來找到這個問題的答案,無論如何請檢查這個[使用jQuery清除](http://stackoverflow.com/questions/1043957/clearing-input型文件 - 使用 - jQuery的)。 – Mippy