Explorar o código

Fix AJAX file submits not working

Alan Lam %!s(int64=3) %!d(string=hai) anos
pai
achega
4d30118938

+ 24 - 8
controllers/practice.php

@@ -108,26 +108,42 @@ if($submit) {
 			$values = $field->values ?? [];
 
 			if($type === "file") {
-				$file = (object) $_FILES[$name];
+				/** @var array $file */
+				$file = $_FILES[$name] ?? null;
+				if($file) {
+					$file_error = $file["error"];
+				}
+				else {
+					$file_error = UPLOAD_ERR_NO_FILE;
+				}
 
 				try {
-					if(!$required && (int) $file->error === UPLOAD_ERR_NO_FILE) continue;
-					if($required && (int) $file->error === UPLOAD_ERR_NO_FILE) {
+					if(!$required && (int) $file_error === UPLOAD_ERR_NO_FILE) {
+						continue;
+					}
+
+					if($required && (int) $file_error === UPLOAD_ERR_NO_FILE) {
 						throw new Exception(__("%s is required.", $label));
 					}
 
 					// Detect upload error
-					if((int) $file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
+					if((int) $file_error !== UPLOAD_ERR_OK) {
+						throw new Exception(HTTP::getUploadErrorString($file_error));
+					}
 
 					// Get image details
-					$size = @getimagesize($file->tmp_name);
-					if(!$size) throw new Exception(__("The uploaded file for %s is not an image.", $label));
+					$size = @getimagesize($file["tmp_name"]);
+					if(!$size) {
+						throw new Exception(__("The uploaded file for %s is not an image.", $label));
+					}
 
 					[$width, $height, $type] = $size;
-					if($type !== IMAGETYPE_JPEG) throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
+					if($type !== IMAGETYPE_JPEG) {
+						throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
+					}
 
 					// Resize image
-					$output_image = Image::normalise($file->tmp_name);
+					$output_image = Image::normalise($file["tmp_name"]);
 
 					// Write image to disk
 					$output_path = $test->getDirectory() . "/" . $name . ".jpg";

+ 24 - 8
controllers/test_tester_form.php

@@ -90,26 +90,42 @@ if($submit) {
 			$values = $field->values ?? [];
 
 			if($type === "file") {
-				$file = (object) $_FILES[$name];
+				/** @var array $file */
+				$file = $_FILES[$name] ?? null;
+
+				if($file) {
+					$file_error = $file["error"];
+				}
+				else {
+					$file_error = UPLOAD_ERR_NO_FILE;
+				}
 
 				try {
-					if(!$required && $file->error === UPLOAD_ERR_NO_FILE) continue;
-					if($required && $file->error === UPLOAD_ERR_NO_FILE) {
+					if(!$required && $file_error === UPLOAD_ERR_NO_FILE) {
+						continue;
+					}
+					if($required && $file_error === UPLOAD_ERR_NO_FILE) {
 						throw new Exception("$label is required.");
 					}
 
 					// Detect upload error
-					if($file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
+					if($file_error !== UPLOAD_ERR_OK) {
+						throw new Exception(HTTP::getUploadErrorString($file_error));
+					}
 
 					// Get image details
-					$size = @getimagesize($file->tmp_name);
-					if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
+					$size = @getimagesize($file["tmp_name"]);
+					if(!$size) {
+						throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
+					}
 
 					[$width, $height, $type] = $size;
-					if($type !== IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
+					if(in_array($type, [IMAGETYPE_JPEG, IMAGETYPE_PNG])) {
+						throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG or PNG."), $label));
+					}
 
 					// Resize image
-					$output_image = Image::normalise($file->tmp_name);
+					$output_image = Image::normalise($file["tmp_name"]);
 
 					// Write image to disk
 					$output_path = $test->getDirectory() . "/" . $name . ".jpg";

+ 1 - 1
engine/template.php

@@ -264,7 +264,7 @@ class Template extends Model {
 		$dt_end = $dt_now->add(1, "minute")->format($format);
 
 		?>
-		<form method="post" id="test-form">
+		<form method="post" enctype="multipart/form-data" id="test-form">
 
 			<div id="test-main-form-container">
 				<div class="alert alert-info">

+ 7 - 3
public/js/app-practice-test.js

@@ -73,12 +73,16 @@ function initialiseTestForm() {
 		$submitStatus.html("Submitting...");
 		$submitButton.attr("disabled", "");
 
-		var data = $form.serialize();
+		const formData = new FormData($form.get(0));
 
 		$.ajax({
 			type: "post",
 			url: window.location,
-			data: data,
+			data: formData,
+			processData: false,
+			contentType: false,
+			async: false,
+			cache: false,
 			success: function(data, text) {
 
 				try {
@@ -122,4 +126,4 @@ function updateDurationText() {
 	var mins = Math.floor(duration / 60000);
 
 	$("#test-duration-span").text(mins + " min. " + secs + " sec.");
-}
+}

+ 6 - 2
public/js/app-test.js

@@ -36,12 +36,16 @@ function initialiseTestForm() {
 
 		$submitButton.prop("disabled", true);
 
-		const data = $form.serialize();
+		const formData = new FormData($form.get(0));
 
 		$.ajax({
 			type: "post",
 			url: window.location,
-			data: data,
+			data: formData,
+			processData: false,
+			contentType: false,
+			async: false,
+			cache: false,
 			success: (response) => {
 				try {
 					if(!response.success && response.message) throw response.message;