AJAX file upload and size limit

This commit is contained in:
2022-07-31 09:17:04 -04:00
parent 8f9bcaa06a
commit 3954822c89
2 changed files with 152 additions and 27 deletions
+67 -21
View File
@@ -280,8 +280,8 @@ class Proj extends Users_Controller {
public function latejob() {
// echo 'oooooooooooooooooooooooooo';
// $data['redirect_to'] = str_replace(base_url(),'',$_SERVER['HTTP_REFERER']);
$data = $this->getSessionArray();
$uploaded = false;
$data = $this->getSessionArray();
$data['redirect_to'] = '/proj/pastdue/';
@@ -318,7 +318,7 @@ class Proj extends Users_Controller {
// print_r( $out );
}
} else if ($_POST && isset($_POST['uploadForm']) && isset($_FILES["fileToUpload"])) {
$message = $this->process_files($_FILES["fileToUpload"]);
list ($message, $uploaded) = $this->process_files($_FILES["fileToUpload"]);
}
$data = $this->load_files($data, $jobID);
@@ -367,26 +367,32 @@ class Proj extends Users_Controller {
public function thisjob() {
$data = $this->getSessionArray();
$data['redirect_to'] = '/proj/active/';
$uploaded = false;
if (!isset($_SESSION['username']) or $_SESSION['username'] == '') {
redirect(home);
} else {
$message = "";
// echo rand(100,9999). " ameye 1";
$ajax = $this->input->post('ajax');
$jobID = $this->input->post('jobID');
// echo rand(100,9999). " ameye 1";
if ($_POST) {
$ajax = $this->input->post('ajax');
$jobID = $this->input->post('jobID');
} else {
$ajax = $this->input->get('ajax');
$jobID = $this->input->get('jobID');
$message = $this->input->get('message');
}
$data['jobID'] = $jobID;
//$mybalance = 10666;
//$data['mybalance'] = $mybalance;
$this->load->model('backend_model');
// echo rand(100,9999). " ameye 2";
// echo rand(100,9999). " ameye 2";
if ($_POST && !isset($_POST['uploadForm'])) {
// echo rand(100,9999). " ameye 2222-2222";
// exit;
// echo rand(100,9999). " ameye 2222-2222";
// exit;
$msgArray = array();
$msgArray['message'] = trim($this->input->post('jobmessage'));
$msgArray['contract'] = $this->input->post('contractId');
@@ -401,7 +407,7 @@ class Proj extends Users_Controller {
$res = $this->backend_model->wrenchboard_api($msgArray, $out);
}
} else if ($_POST && isset($_POST['uploadForm']) && isset($_FILES["fileToUpload"])) {
$message = $this->process_files($_FILES["fileToUpload"]);
list ($message, $uploaded) = $this->process_files($_FILES["fileToUpload"]);
}
$data = $this->load_files($data, $jobID);
@@ -452,12 +458,17 @@ class Proj extends Users_Controller {
/* $this->load->view('users/view_header_user', $data);
$this->load->view('jobs/view_thisjob', $data);
$this->load->view('users/view_footer_user', $data); */
$this->RenderUserPage('jobs/view_thisjob', $data);
if ($uploaded) {
redirect('/proj/thisjob?jobID='.$jobID.'&message='.$message);
} else {
$this->RenderUserPage('jobs/view_thisjob', $data);
}
}
}
}
public function mycompltjob() {
$uploaded = false;
$data = $this->getSessionArray();
if (!isset($_SESSION['username']) or $_SESSION['username'] == '') {
@@ -493,7 +504,7 @@ class Proj extends Users_Controller {
// print_r( $out );
}
} else if ($_POST && isset($_POST['uploadForm']) && isset($_FILES["fileToUpload"])) {
$message = $this->process_files($_FILES["fileToUpload"]);
list ($message, $uploaded) = $this->process_files($_FILES["fileToUpload"]);
}
$this->load->model('dash_model');
@@ -546,7 +557,8 @@ class Proj extends Users_Controller {
public function compltjob() {
// echo 'oooooooooooooooooooooooooo';
$data = $this->getSessionArray();
$uploaded = false;
$data = $this->getSessionArray();
if (!isset($_SESSION['username']) or $_SESSION['username'] == '') {
redirect(home);
@@ -581,7 +593,7 @@ $data = $this->getSessionArray();
// print_r( $out );
}
} else if ($_POST && isset($_POST['uploadForm']) && isset($_FILES["fileToUpload"])) {
$message = $this->process_files($_FILES["fileToUpload"]);
list ($message, $upaloded) = $this->process_files($_FILES["fileToUpload"]);
}
$this->load->model('dash_model');
@@ -649,7 +661,7 @@ $data = $this->getSessionArray();
//$detail_button = " '<button type=\"button\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#modal_theme_primary\">Delvery Detail<i class=\"icon-play3 position-right\"></i></button>' ";
$detail_button =" '<a href=\"#\" onclick=\"showJobModal('''||mc.contract||''');\" >Delivery Detail</a>' ";
//$detail_button = " '<a href=\"#modal_theme_primary\" class=\"label label-flat border-primary text-primary-600\" data-target=\"#modal_theme_primary\">Linked label</a>' "; // <button type=\"button\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#modal_theme_primary\">Delvery Detail<i class=\"icon-play3 position-right\"></i></button>' ";
//$detail_button = " '<a href=\"#modal_theme_primary\" class=\"label label-flat border-primary text-primary-600\" data-target=\"#modal_theme_primary\">Linked label</a>' "; // <button type=\"button\" class=\"btn btn-primary btn-xs\" data-toggle=\"modal\" data-target=\"#modal_theme_primary\">Delvery Detail<i class=\"icon-play3 position-right\"></i></button>' ";
$is_live = $this->backend_model->cfgReadChar("system.live");
@@ -710,7 +722,7 @@ $data = $this->getSessionArray();
$this->table->set_template($this->template_nohead);
$this->table->set_heading('');
$data['message_table'] = $this->table->generate($query_message);
//===================
//===================
$data['message_result'] = $query_message->result();
@@ -1024,11 +1036,11 @@ $data = $this->getSessionArray();
$data['new_message'] = $out['new_message'];
/*
/*
$this->load->view('users/view_header_user', $data);
$this->load->view('jobs/view_mycompletejob', $data);
$this->load->view('users/view_footer_user', $data);
*/
*/
$data['page_title'] ="Previous Tasks";
$this->renderSecurePage('jobs/view_mycompletejob', $data);
}
@@ -1038,6 +1050,7 @@ $data = $this->getSessionArray();
* This is the file upload function
*/
private function process_files($files) {
$uploaded = false;
$result = "";
$d_txt = "";
$is_live = $this->backend_model->cfgReadChar("system.live");
@@ -1066,11 +1079,44 @@ $data = $this->getSessionArray();
}
}
$result = $i > 0 ? "Uploaded file(s): ${i}" : "Failed to process uploaded files${d_txt}";
$uploaded = $i > 0;
} else {
$result = "Files were not uploaded${d_txt}";
}
// echo $result;
return $result;
// echo $result;
return array($result, $uploaded);
}
public function upload() {
$data = $this->getSessionArray();
$jobID = $this->input->post('jobID');
$contractId = $this->input->post('contractId');
$files = array(
"name" => array($_FILES["fileToUpload"]["name"]),
"error" => array($_FILES["fileToUpload"]["error"]),
"tmp_name" => array($_FILES["fileToUpload"]["tmp_name"]),
"size" => array($_FILES["fileToUpload"]["size"]),
"type" => array($_FILES["fileToUpload"]["type"])
);
list ($message, $uploaded) = $this->process_files($files);
if ($uploaded) {
header("Content-Type:application/json");
echo json_encode(
array(
"status"=>1,
"message"=> ($message != '' ? $message : "File upload failed with an unexpected error.")
));
} else {
header("Content-Type:application/json");
echo json_encode(
array(
"status"=>0,
"message"=> ($message != '' ? $message : "Sorry, there was an error uploading your file.")
));
}
}
}
@@ -57,11 +57,11 @@
<!--begin::Controls-->
<div class="dropzone-panel mb-4">
<!-- a class="dropzone-select btn btn-sm btn-primary me-2">Attach files</a -->
<input class="dropzone-select btn btn-sm btn-primary me-2" type='file' name='fileToUpload[]' multiple="" id="fileToUpload[]" onchange="fileToUploadChanged(this)">
<input class="dropzone-select btn btn-sm btn-primary me-2" type='file' name='fileToUpload[]' multiple="" id="fileToUpload" onchange="fileToUploadChanged(this)">
<!-- a class="dropzone-upload btn btn-sm btn-light-primary me-2">Upload All</a>
<a class="dropzone-remove-all btn btn-sm btn-light-primary">Remove All</a -->
<br><div id="fileToUploadList"></div>
<br><input class="dropzone-select btn btn-sm btn-primary me-2" type='submit' value='Upload' name='upload' />
<br><input class="dropzone-select btn btn-sm btn-primary me-2" type='button' value='Upload' name='upload' onclick="fileToUploadRun(); return false;" />
</div>
<!--end::Controls-->
<!--begin::Items-->
@@ -119,12 +119,91 @@
<script type="text/javascript">
<!--
const sizeLimit = <?php echo '3145728'; ?>;
function fileToUploadChanged(e) {
var fileToUploadList = document.getElementById('fileToUploadList');
var fileList = $('#fileToUpload').prop("files"); // e.files;
$('#fileToUploadList').html('');
fileToUploadList.innerHTML = '';
e.files.forEach(file => {
//alert(file.name);
fileToUploadList.innerHTML += '<br><b>' + file.name + '</b> - ' + humanFileSize(file.size) + ' (' + file.type + ')';
var i;
for ( i = 0; i < fileList.length; i++) {
if (fileList[i].size > sizeLimit) {
$('#fileToUploadList').append('<br>Size limit exceeded: <span style="color:red;"><b>' + fileList[i].name + '</b> - ' + humanFileSize(fileList[i].size) + ' (' + fileList[i].type + ')</span>');
} else {
$('#fileToUploadList').append('<br><b>' + fileList[i].name + '</b> - ' + humanFileSize(fileList[i].size) + ' (' + fileList[i].type + ') <span class="loading-prep" id="prog'+i+'"></span>');
}
}
}
function fileToUploadRun() {
var fileList = $('#fileToUpload').prop("files");
var i, n = 0;
for ( i = 0; i < fileList.length; i++) {
if (fileList[i].size <= sizeLimit) {
n = i;
}
}
if (n < 1) {
alert('No files for upload!');
return;
}
console.log('Max upload index is ' + n);
for ( i = 0; i <= n; i++) {
if (fileList[i].size <= sizeLimit) {
console.log('Uploading file ' + i + ' of ' + n + ' => ' + fileList[i].name);
uploadajax(n, i);
}
}
}
function uploadajax(ttl,cl) {
var fileList = $('#fileToUpload').prop("files");
$('#prog'+cl).removeClass('loading-prep').addClass('upload-image');
var form_data = "";
form_data = new FormData();
form_data.append("fileToUpload", fileList[cl]);
form_data.append("jobID",'<?php echo $jobID; ?>');
form_data.append("contractId",'<?php echo $contract_id; ?>');
var request = $.ajax({
url: "/proj/upload",
cache: false,
contentType: false,
processData: false,
async: true,
data: form_data,
type: 'POST',
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if(xhr.upload){
xhr.upload.addEventListener('progress', function(event){
var percent = 0;
if (event.lengthComputable) {
percent = Math.ceil(event.loaded / event.total * 100);
}
$('#prog'+cl).text(percent+'%')
}, false);
}
return xhr;
},
success: function (res, status) {
if (status == 'success') {
percent = 0;
$('#prog' + cl).text('');
$('#prog' + cl).text('--Success: ');
if (cl < ttl) {
uploadajax(ttl, cl + 1);
} else {
alert('Done');
$('#fileToUpload').val(null);
$('#fileToUploadList').html('');
}
}
},
fail: function (res) {
alert('Failed');
}
});
}