简体   繁体   中英

Send a file generated from table2excel.js using ajax to save it on server

I have a HTML table from which i am generating a .xls file through Table2Excel.js plugin. I am trying to post this file to server so that i can save it at the server using php. I am trying to send the file in blob format, but my $_POST is returning empty. Heres my code for ajax:

  var blob = new Blob([e.format(fullTemplate, e.ctx)], { type: "application/vnd.ms-excel" });
            var dob = "okay lets check";
            window.URL = window.URL || window.webkitURL;
            link = window.URL.createObjectURL(blob);
            a = document.createElement("a");
            a.download = getFileName(e.settings);
            a.href = link;
            document.getElementById("getPDF").value = link;
            document.body.appendChild(a);

            a.click();

            document.body.removeChild(a);
            var formdata = new FormData();
            formdata.append('excelFile', blob);
            $.ajax({
              url:'savetoserver.php',
              type:'post',
              data:formdata,
              processData: false,
              contentType: false,
            }).done(function (data){
              alert(data);
            }).fail(function (xhr, status, err) {
              alert(err);
            });

and php code is basically atm

<?php print_r($_POST); ?>

You need to use $_FILES in order to receive the file in PHP. here is an example PHP-script.

if (0 < $_FILES['excelFile']['error'])
    echo 'Error: ' . $_FILES['excelFile']['error'];
else
    move_uploaded_file ($_FILES['excelFile']['tmp_name'], $_FILES['excelFile']['name']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM