I've recently started using angularjs for file uploads and came across this helpful model on github: https://github.com/danialfarid/angular-file-upload
The file upload process is working smoothly for me. However, I'm facing a challenge after uploading the file - I want to retrieve the file contents in JSON format and then iterate over the JSON object using AngularJS. Unfortunately, I'm unsure of how to achieve this.
Below is the snippet of 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";
}
//How do I return as JSON here?
Also, 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 uploaded successfully
console.log(data);
$scope.result = data;
});
}
});
I am trying to convert the data into a JSON object. Can anyone guide me on how I can accomplish this? My attempts with json_encode in PHP have been unsuccessful so far.
If anyone has insights on this matter, I would greatly appreciate your help.