How to upload xlsx file with angular and php?

Asked

Viewed 787 times

0

Hello,

I am developing a financial system for client companies that I already own. I am using angular and php. In this system, I have a part that needs to import data in excel. I’m having trouble getting the file and the company ID to send the comic. NOTE: I already have the php code that moves the file to another folder and turns the xlsx into csv.

<button type="file" ngf-select="uploadFiles($file, $invalidFiles)"
accept="*/*" ngf-max-height="1000" ngf-max-size="1MB">Select File</button>

<?php
include_once("con.php");

$pdo = conectar();

$id_empresa = $_POST['id_empresa'];

include_once("PHPExcel/Classes/PHPExcel.php");

$uploadDir = "uploadFile/";

$uploadfile = $uploadDir . $_FILES['arquivo']['name'];

if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $uploadfile)) {
   echo "Dados pegos com sucesso.";
}else{
   echo "Não foi possível pegar arquivo";
}
?>

JS

app.controller('AppController', ['$scope', '$window', 'Upload', function($scope, $window, Upload) {

    $scope.uploadFiles = function(file, errFiles) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        file.id_empresa = $window.localStorage.getItem("idemp");
        id_empresa = $window.localStorage.getItem("idemp");

        if (file) {
            console.log(file);
            file.upload = Upload.upload({
                url: 'http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/importaArquivo.php',
                method: 'POST',
                data: {'id_empresa': id_empresa}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                    file.result = response.data;
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                                     evt.loaded / evt.total));
            });
        }   
    }


}]);
  • Where does the id_company come from? What comes from $_FILES['arquivo']['name']? It is appropriate to pass this variable through basename to get only the file name.

  • The id_company comes from the angular controller. Already in $_FILES['file']['name'], it doesn’t come come, because I can’t catch it at the angle and go to php, understand @Foxtrot? This $_FILES is from an old system that is only in php

  • Your problem is clearly in the javascript part, please post the function salvarArquivo so we can see how this one looks. And please read this https://answall.com/help/mcve, whenever you ask ask ask for an example that we can reproduce the problem, you assumed the problem was in PHP, but between the client-side and server-side there could be many things, then always follow the link tips to help people help you

  • I put my javascript codes @Guilhermenascimento. I actually know that the problem is in javascript itself. Sorry I didn’t put the.

  • This Javascript does nothing :/ only generates the console.log, if I understand the problem you are confusing Ajax and normal upload.

  • Yes, because I want to see what comes in payload. I want to know if the file is coming.

Show 1 more comment

1 answer

0

I get it. Since you have it, it doesn’t work.

Or you choose to use only native HTML elements, and so when you do Submit is sent by POST the file, like this:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="arquivo" id="arquivo">
    <input type="submit" value="Salvar conta" name="submit">
</form>

This way, it is not Angularjs to receive the file and have to re-upload it.

If you really want to use Angularjs, you have for example the ng-file-upload where you can pre-validate on the client side, such as the file size or type, and then you can use the Upload.upload() to re-upload all data to the server. You have here an example of use.

  • Sorry, but I found this example very complex. I mean, the JS part.

  • I will try to follow the examples.

  • I’m getting it. The problem now is how to get the file and id, which I’m passing, in php

  • I can already send the file to php, but I can’t catch it. And I believe I’m sending from js to php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.