我正在尝试上传文本文件。我希望在文件上传之前向用户发出警报此文件包含的多少记录(不匹配),这些记录尚未在数据库中。我正在尝试获取它(计数) > 0).file上传工作正常。但我正在努力与alert.how我可以这样做。
这是我的代码
控制器
public function uploadAttendanceFile(Request $request)
{
if ($request->hasFile('file')) {
$file = $request->file('file');
$name = $file->getClientOriginalName();
$attendanceSheet = new AttendanceSheet();
$attendanceSheet->name = $name;
$attendanceSheet->created_by = Auth::user()->id;
$attendanceSheet->modified_by = Auth::user()->id;
$attendanceSheet->save();
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)) {
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);
while (($line = fgetcsv($csvFile)) !== FALSE) {
$date = implode(" ", $line);
$x = mb_substr($date, 0, 20);
$y = mb_substr($date, 41, 60);
$epf = str_replace(' ', '', $x);
$fdate = array_filter(explode(" ", $y));
/*dd($fdate);*/
$a_date = date("Y-m-d", strtotime($fdate[9]));
$sql = "SELECT * from attendanceLogs where emp_id ='$epf' AND date ='$a_date' AND time='$fdate[10]' AND session='$fdate[11]'";
$result = DB::select(DB::raw($sql));
$count = count($result);
if ($count > 0) {
return redirect()->back()->with('message', 'IT WORKS!');
}
else {
$csv = new AttendanceLogs();
$csv->emp_id = $epf;
$csv->date = $a_date;
$csv->time = $fdate[10];
$csv->session = $fdate[11];
if (empty($fdate[14])) {
$csv->machine = $fdate[15];
} elseif (empty($fdate[15])) {
$csv->machine = $fdate[14];
}
$csv->created_by = Auth::user()->id;
$csv->save();
}
}
}
}
$file->move(base_path('public/attendance_sheets'), $file->getClientOriginalName());
$contents = File::get(base_path('public\attendance_sheets\\' . $name));
}
}
刀片
<div class="page-content" ng-controller="uploadAttendanceSheetCtrl">
<div class="col-md-offset-3 col-md-9">
<button type="button" ng-click = "uploadFile()" id="test" class="btn medium blue" disabled>Upload</button>
</div>
</div>
Angular js
app.service('fileUpload', ['$http', 'toaster', function ($http, toaster) {
this.uploadFileToUrl = function (file, uploadUrl) {
/*toaster.pop('wait', "Uploading");*/
var fd = new FormData();
fd.append('file', file);
return $http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function () {
toaster.clear();
toaster.pop('info', "Notificaton", "Attendance Sheet is uploaded successfully", 4000, 'trustedHtml');
})
.error(function () {
});
}
}]);
app.controller('uploadAttendanceSheetCtrl', ['$scope', '$http', 'fileUpload', 'toaster', function ($scope, $http, fileUpload, toaster) {
$scope.loadUploadedFiles = function () {
//var year = new Date().getFullYear();
$http({
method: "GET",
url: "/attendance/loadUploadedFiles",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
}).then(function successCallback(response) {
$scope.uploadedFiles = response.data;
}, function errorCallback(response) {
});
};
$scope.loadUploadedFiles();
$scope.uploadFile = function () {
var file = $scope.attendanceSheet;
var uploadUrl = "/attendance/uploadAttendanceFile";
fileUpload.uploadFileToUrl(file, uploadUrl).then(function () {
$scope.loadUploadedFiles();
});
};
}]);