jsp Click Picture Pop up File Upload Interface and Realize Detailed Explanation of Preview Example

  • 2021-12-04 10:51:49
  • OfStack

jsp Click Picture Pop-up File Upload Interface and Implementation Preview Example Detailed Explanation

It took two days to ponder the function of picture preview

The task requirements are as follows:

1: There is a picture in the jsp page (pic_1)

2: Click on the picture to pop up an interface similar to Explorer

3: Preview at pic_1 after selecting a certain picture

I in IE8 test the following code, can achieve the above functions, not in other browsers test, if you know a variety of browser support methods, please comment, learn together, thank you.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Insert title here</title> 
<script type="text/javascript"> 
function tempClick(){ 
  *  Firefox browser realizes file uploading interface by clicking pictures  
  * var a=document.createEvent("MouseEvents"); 
  * a.initEvent("click", true, true); 
  * document.getElementById("upload_main_img").dispatchEvent(a); 
 //IE The browser realizes the file upload interface by clicking on the picture  
 document.getElementById('main_img').click();   // Call main_img Adj. onclick Events  
 *  Preview Picture  
 * @param obj 
 * @returns {Boolean} 
function PreviewImg(obj) { 
 var newPreview = document.getElementById("img_2"); 
 var imgPath = getPath(obj); 
 if( !obj.value.match( /.jpg|.gif|.png|.bmp/i ) ){ 
  alert(" The picture format is wrong! "); 
  return false; 
 newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";  
 newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgPath; 
 *  Get the absolute path of the picture  
 * @param obj 
 * @returns 
function getPath(obj){ 
 if(obj) {  
  if(navigator.userAgent.indexOf("MSIE")>0) { 
   //IE Get the local path of the picture under   
   return document.selection.createRange().text; 
  } else if(isFirefox=navigator.userAgent.indexOf("Firefox")>0) { 
    if (obj.files) { // Firefox What is obtained under is the data of the picture   
    return files.item(0).getAsDataURL();  
    return obj.value;  
   return obj.value;  
<div><input type="file" style="position: absolute; filter: alpha(opacity = 0); opacity: 0; width: 30px;" size="1" id="main_img" name="main_img" onchange="PreviewImg(this)"></div> 
<div id="img_2" style="width:133px;height:95px; cursor:pointer; background-image: url('Chrysanthemum.jpg');" onclick="tempClick()"></div> 

Simply explain the code as follows:

input type= "file" This element I tried to hide style (style= "display: none"), so that I can't get an absolute path, but fakepath. In order not to show it, let it be 100% transparent. Here is an div, id is img_2, and this div is the initial picture of the page. Click this picture to call the method in ES40type= "file" to realize this function.

Thank you for reading, hope to help everyone, thank you for your support to this site!

Related articles: