-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrevisualizarImagen.html
47 lines (32 loc) · 1.17 KB
/
PrevisualizarImagen.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Previsualizar imagen</title>
</head>
<body>
<h1>Previsualización de imagen</h1>
<img id="imgPreview" width="150px" height="150px">
<form action="#" method="POST" enctype="multipart/form-data">
<input type="file" accept="image/*" onchange="previewImage(event, '#imgPreview')">
<button>Enviar formulario</button>
</form>
<script>
function previewImage(event, querySelector) {
//Recuperamos el input que desencadeno la acción
const input = event.target;
//Recuperamos la etiqueta img donde cargaremos la imagen
$imgPreview = document.querySelector(querySelector);
// Verificamos si existe una imagen seleccionada
if (!input.files.length) return
//Recuperamos el archivo subido
file = input.files[0];
//Creamos la url
objectURL = URL.createObjectURL(file);
//Modificamos el atributo src de la etiqueta img
$imgPreview.src = objectURL;
}
</script>
</body>
</html>