JSFiddle - React, Tailwind, and code Playground
by NicosKaralis
HTML
<html>
<head>
<title>Upload de Imagem</title>
</head>
<body>
<form name="form" enctype="multipart/form-data" method="post" action="javascript:verifica_extencao()" id="form">
<p>Escolha a foto:</p>
<p><input type="file" name="arq" size="20" class="form2" id="#{id:fileUpload2}"></p>
</form>
<p><a href="javascript:verifica()">GRAVAR!!</a></p>
<p> </p>
<p> </p>
<div id="foto"></div>
</body>
</html>
JavaScript
function verifica(){
var fileName = document.getElementById("#{id:fileUpload2}").value;
if(fileName == "" || fileName == null) {
alert("Por favor escolha um arquivo antes de carregá-lo.");
return false;
}
alert(filename);
var picPostfix = ["jpeg", "gif","jpg"];
var lastPos = fileName.lastIndexOf(".");
var fileExt = fileName.substr(lastPos+1);
var fileNameMod = replaceAll(fileName, "\\", "/");
for(var i=0; i<picPostfix.length; i++) {
var ext = picPostfix[i];
if(fileExt == ext) {
fileName = "file:///" + fileNameMod;
alert(fileName);
var newImg = new Image();
newImg.src = fileName;
var height = newImg.height;
var width = newImg.width;
alert("Altura "+height);
alert("Largura "+width);
if(width > 800 || height > 600){
alert("A resolução da imagem deve ser menor ou igual a 800x600");
return false;
}
return true;
}
}
alert("Selecione um arquivo de imagem(.jpeg,.gif,.jpg)");
return false;
}
function replaceAll(str, de, para){
var pos = str.indexOf(de);
while (pos > -1){
str = str.replace(de, para);
pos = str.indexOf(de);
}
return (str);
}