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

230
Vistas
how to download and save file with XMLHttpRequest and PHP

I need to receive the file via XMLHttpRequest and then save this file in a .zip file. I tested it by sending the response via XMLHttpRequest but it didn't work very well, does anyone know how to do this? i need download and no upload file

$(".download_files").click(function () {
    let ajax = new XMLHttpRequest();
    ajax.open("POST", "http://localhost/download.php");
    ajax.onprogress = function (ev) {
        $(".total").text(ev.total);
        $(".loaded").text(ev.loaded);
        $(".p").text(Math.floor(ev.loaded / ev.total * 100));
    }
    ajax.onloadend = function (ev) {
        let obj = this;
        if (obj.status === 200) {
            $(".create").show();
            let ajaxCreate = new XMLHttpRequest();
            ajaxCreate.open("POST", "test.php");
            ajaxCreate.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            ajaxCreate.send("data=" + obj.response);
            console.log(obj.response);
        } else {
            alert("download error :/");
        }
    }
    ajax.send();
});

PHP code

<?php

$data = $_POST['data'];

$fopen = fopen(__DIR__."/downloads/".sha1(time()+mt_rand()).".zip", "w+");
fwrite($fopen, $_POST['data']);
fclose($fopen);
about 4 years ago · Juan Pablo Isaza
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