Click the image popup file upload interface and preview function in jsp
- 2020-06-15 10:05:15
- OfStack
I spent two days working on 1 image preview feature
Task requirements are as follows:
1: jsp page with 1 image (pic_1)
2: Click the picture to pop up the interface similar to the resource manager
3: Preview at pic_1 after selecting an image
I am in IE8 to try the following code, can achieve the above functions, not in other browsers to test, if you know a variety of browsers support methods, please give instructions, learn together, thank you.
A simple explanation of the code:
input type="file" element I tried to make style hidden (style= "display:none"), so that it could not or would get absolute path, but fakepath. In order not to show it, so that it is 100% transparent, there is 1 div below, id is img_2, this div is the initial image of the page, click this image to call the method in input type="file" to achieve this function.
My ability is limited. Please advise me. If you have a better method, please offer it to me.
Task requirements are as follows:
1: jsp page with 1 image (pic_1)
2: Click the picture to pop up the interface similar to the resource manager
3: Preview at pic_1 after selecting an image
I am in IE8 to try the following code, can achieve the above functions, not in other browsers to test, if you know a variety of browsers support methods, please give instructions, learn together, thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function tempClick(){
/**
* Firefox browser to click on the image appear file upload interface
* var a=document.createEvent("MouseEvents");
* a.initEvent("click", true, true);
* document.getElementById("upload_main_img").dispatchEvent(a);
*/
//IE Browser to click on the image to upload files interface
document.getElementById('main_img').click(); // call main_img the onclick The event
}
/**
* The preview image
* @param obj
* @returns {Boolean}
*/
function PreviewImg(obj) {
var newPreview = document.getElementById("img_2");
var imgPath = getPath(obj);
alert(imgPath);
if( !obj.value.match( /.jpg|.gif|.png|.bmp/i ) ){
alert(" Picture format error! ");
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 image
* @param obj
* @returns
*/
function getPath(obj){
if(obj) {
if(navigator.userAgent.indexOf("MSIE")>0) {
obj.select();
//IE Get the local path of the image
return document.selection.createRange().text;
} else if(isFirefox=navigator.userAgent.indexOf("Firefox")>0) {
if (obj.files) { // Firefox And then I'm going to get the data for the picture
return files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}
</script>
</head>
<body>
<form>
<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>
</form>
</body>
</html>
A simple explanation of the code:
input type="file" element I tried to make style hidden (style= "display:none"), so that it could not or would get absolute path, but fakepath. In order not to show it, so that it is 100% transparent, there is 1 div below, id is img_2, this div is the initial image of the page, click this image to call the method in input type="file" to achieve this function.
My ability is limited. Please advise me. If you have a better method, please offer it to me.