Foros del Web » Programando para Internet » Javascript »

Upload foto.

Estas en el tema de Upload foto. en el foro de Javascript en Foros del Web. hola yo ocupo html -> php para subir un archivo. ahora estoy intentando subir una foto en este caso pero me marca un error: formulario: ...
  #1 (permalink)  
Antiguo 17/03/2021, 18:34
Avatar de mveraa  
Fecha de Ingreso: diciembre-2002
Ubicación: santiago-chilito
Mensajes: 1.931
Antigüedad: 21 años, 3 meses
Puntos: 2
Upload foto.

hola yo ocupo html -> php para subir un archivo. ahora estoy intentando subir una foto en este caso pero me marca un error:

formulario:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Resize Images Before Upload</title>


<link rel="stylesheet" href="./bootstrap.min.css" crossorigin="anonymous">



</head>
<body>

Select a file: <input type="file" onchange='resize()' id="uploader">
<button onclick='resize()'>Resize</button>
<img id="image">

<script>

function resize(){
//define the width to resize e.g 600px
var resize_width =200;//without px

//get the image selected
var item = document.querySelector('#uploader').files[0];

//create a FileReader
var reader = new FileReader();

//image turned to base64-encoded Data URI.
reader.readAsDataURL(item);
reader.name = item.name;//get the image's name
reader.size = item.size; //get the image's size
reader.onload = function(event) {
var img = new Image();//create a image
img.src = event.target.result;//result is base64-encoded Data URI
img.name = event.target.name;//set name (optional)
img.size = event.target.size;//set size (optional)
img.onload = function(el) {
var elem = document.createElement('canvas');//create a canvas

//scale the image to 600 (width) and keep aspect ratio
var scaleFactor = resize_width / el.target.width;
elem.width = resize_width;
elem.height = el.target.height * scaleFactor;

//draw in canvas
var ctx = elem.getContext('2d');
ctx.drawImage(el.target, 0, 0, elem.width, elem.height);

//get the base64-encoded Data URI from the resize image
var srcEncoded = ctx.canvas.toDataURL(el.target, 'image/jpeg', 0);

//assign it to thumb src
document.querySelector('#image').src = srcEncoded;

/*Now you can send "srcEncoded" to the server and
convert it to a png o jpg. Also can send
"el.target.name" that is the file's name.*/

//get the resized image from src
var resized = document.querySelector('#image').src;

//note: remember that the image is now base64-encoded Data URI

//sendind the image to the server (php)
var fd = new FormData();
fd.append("image", resized);

//sending data to the server
var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function()
{


if (xhr.readyState==4 && xhr.status==200)
{
var response = JSON.parse(xhr.responseText);

console.log("RESPUESTA:"+response);
if(response.success == true)
{
alert('The image was uploaded');
}
}
}
xhr.open("POST", "upload1975.php");
xhr.send(fd);




}
}
}


</script>
</body>
</html>


al momento de seleccionar al foto entiendo que envía el archivo upload1975.php-

Error:
VM233:1 Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at XMLHttpRequest.xhr.onreadystatechange (prueba_foto3.php:78)
at Image.img.onload (prueba_foto3.php:92)

espero que alguien me pueda orientar en mi error.
__________________
"Cuando se adelanta un oponente, enfréntalo y salúdalo; si intenta retroceder, déjalo seguir su camino"
  #2 (permalink)  
Antiguo 18/03/2021, 09:13
 
Fecha de Ingreso: abril-2006
Mensajes: 583
Antigüedad: 18 años
Puntos: 120
Respuesta: Upload foto.

ahi te dice, error de sintaxis , en resumen tu archivo "prueba_foto3.php" no esta devolviendo un JSON valido. tendras que aprender a usar el "Inspector" de tu navegador de escritorio para que depures mejor el codigo. lo mas seguro es que en el lado de PHP se esta produciendo algun error y ese php te esta mostrando el error pero tu esperas un JSON o un mensaje de error de PHP
__________________
Mis aportes te ayudaron??, te hicieron ahorrar valiosos tiempo??, si quieres puedes agradecerme con un Gracias o con una donacion
https://paypal.com/pools/c/8lmNLmWnG9
  #3 (permalink)  
Antiguo 18/03/2021, 19:22
Avatar de mveraa  
Fecha de Ingreso: diciembre-2002
Ubicación: santiago-chilito
Mensajes: 1.931
Antigüedad: 21 años, 3 meses
Puntos: 2
Respuesta: Upload foto.

ok. grcs. por la orientacion
__________________
"Cuando se adelanta un oponente, enfréntalo y salúdalo; si intenta retroceder, déjalo seguir su camino"

Etiquetas: input, js, upload
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:49.