0

I'm using angularjs to upload files. Im using this model that I've found at github: https://github.com/danialfarid/angular-file-upload

The upload works perfect. However, after I've uploaded the file, I want to return the file contents as JSON, and then iterate of the JSON-object with Angular. But I don't know how to do this.

Here is my code:

$filename = $_FILES['file']['tmp_name'];
$csv = array_map('str_getcsv', file($filename));
foreach($csv as $c)
{
    echo str_replace(array('"', ';'), ' ', $c[0]) . "\n";
}
    //Return as JSON here? HOW?

Here is my controller:

as.controller('Marketing', function($scope, $http, $upload)
{
    $scope.onFileSelect = function($files) {
        var file = $files[0];
        if (file.type.indexOf('image') == -1) {
            $scope.error = 'image extension not allowed, please choose a JPEG or PNG file.'            
        }
        if (file.size > 2097152){
            $scope.error ='File size cannot exceed 2 MB';
        }     
        $scope.upload = $upload.upload({
            url: 'partials/result.php',
            data: {},
            file: file,
        }).success(function(data, status, headers, config) {
            // file is uploaded successfully
            console.log(data);
            $scope.result = data;
        });
    }
});

I want the data to be and JSON-object. How can I accomplish this? When I try json_encode with PHP, it does not work.

Anyone who can help me with this?

user500468
  • 1,183
  • 6
  • 21
  • 36
  • Do you have an example of the CSV data you sent to the server? Does it have structure? – Dean Ward Jul 04 '14 at 09:57
  • Check this [How to retrieve JSON data from server](http://stackoverflow.com/questions/24468459/sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery/24468752#24468752) – hex494D49 Jul 04 '14 at 09:58
  • @hex494D49: I can't get it right. – user500468 Jul 04 '14 at 11:16
  • @user500468 Well, I see you're trying to retrieve an (just) uploaded binary file as a JSON object but I'm not sure if you can do that. I'm afraid that's not possible to retrieve a binary file in JSON format. Try to open a .jpg with Notepad and check what's inside - never tried but I believe this code `echo json_decode(stripslashes(file_get_contents("php://input")))` would produce something similar or it would throw an weird error. – hex494D49 Jul 04 '14 at 11:32
  • @hex494D49: So I can't return the content of the file as JSON, because I want that. – user500468 Jul 04 '14 at 11:35
  • @user500468 I won't be exclusive so I'll check it out in a meantime and post back the results here :) – hex494D49 Jul 04 '14 at 11:37
  • @hex494D49: Any idea? – user500468 Jul 04 '14 at 13:27
  • @user500468 Check my answer now. Your title was quite misleading :) I just saw this is about CSV file, which is a textual file 'cause I thought you where dealing with binary files, which you can't convert to JSON. – hex494D49 Jul 04 '14 at 13:30

1 Answers1

0

I believe this is what you're looking for

if(isset($_FILES["file"])){
    $fname = $_FILES['file']['tmp_name'];
    // ...
    if(move_uploaded_file($fname, "uploads/" . $fname)){

        // this way
        $csv = file_get_contents($fname);
        $array = array_map("str_getcsv", explode("\n", $csv));
        echo json_encode($array);

        // or this way; have in mind delimiter
        $row = str_getcsv($csv, "\n");
        $length = count($row);
        for($i = 0; $i < $length; $i++){
            $data = str_getcsv($row[$i], ";");
            $array[] = $data; 
        }
        echo json_encode($array);
    }
    // ...
}
hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • When I check the response in firebug, I can't see that the returned data is in JSON? This is returned: http://pastebin.com/KHPmD97r as you can see, it is not in the typical json-format. Has it something to do with the request, should it be GET request instead of POST? – user500468 Jul 04 '14 at 13:36
  • @user500468 I just made a CSV in Excel, checked it and it works. Can you post a fragment of your CSV file and I'll check it again. I'll try these data you already uploaded. – hex494D49 Jul 04 '14 at 13:43
  • Here you can see my response: http://postimg.org/image/fpp7ksc1d/ and as you can see, it is not in JSON-format – user500468 Jul 04 '14 at 13:50
  • @user500468 Very strange, I'm getting a series of nulls, but I'm not giving up :) – hex494D49 Jul 04 '14 at 14:06
  • You are getting nulls from what? :) – user500468 Jul 04 '14 at 14:11
  • @user500468 Check the updated answer, the `second attempt` part. – hex494D49 Jul 04 '14 at 14:34
  • @hex49D49: Still no JSON:/. The response is still text/html. Thanks anyway :) – user500468 Jul 04 '14 at 14:44