0

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);

0 Answers0