Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Download image from a php function called with ajax

I have this ajax:

 $('.getid').on('click', function(){
        var imagename =  $(this).data("id");
        // you can make ajax call here to get data
        $.ajax({
        type: "POST",
        url: "Monitor/downloadDesiredImage",
        data: {'val' : imagename},
        dataType: "text"
        }).done(function(data) {
       console.log(data);        
       }).fail(function() {
    alert( "error" );
  });
});

This will work with a button that will send on.click a id value saved in a var imagename, that is a string that correspond to a image name saved on my server.

This is the php function that will get that id :

public function downloadDesiredImage() {
        $imagename = $_POST['val'];
        echo "this is my ".  $imagename;
        $file = file_get_contents('./images/'.$imagename.'.jpg', FILE_USE_INCLUDE_PATH);
        $imageData = base64_encode(($file));
        $src = 'data: '.mime_content_type($file).';base64,'.$imageData;
        echo '<img src="' . $src . '">';

    }

That will output the image in the network response, but I will also get the error :

Message: mime_content_type(): Invalid path

But what I want to do is after I click the button is to download that image from my server. How I can do that ? Thanks!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

mime_content_type() expects the file name, not the data of the file itself. Something like this:

mime_content_type('./images/'.$imagename.'.jpg')
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda