Page 1 of 1

Uppy 401 Unauthorized

Posted: Fri May 23, 2025 1:36 pm
by vario
HI,

I am getting a 401 Unauthorized when running an Uppy file upload. The upload works, but it gets executed 4 times and then shows a failure with HTTP code 401. I don't see anything wrong in either the browser console, or the server logs (unless I'm not looking the right places).

I am using an install with the latest github master.zip - would this be a problem?

Neil.

Re: Uppy 401 Unauthorized

Posted: Fri May 23, 2025 2:27 pm
by kev1n
Hi,

Are you using the default upload procedure NUUPLOADFILE_TEMPLATE?
Have you made any modifications to the template code?

I'm not seeing this error when using the latest version from GitHub.

Re: Uppy 401 Unauthorized

Posted: Fri May 23, 2025 4:37 pm
by vario
Kevin,

Not using the default procedure, but there's not much difference as I've based it on the code in nuBuilder-Code-Library repository. Here it is:

Code: Select all

$allowed = array('csv');
$maxfilesize = 5 * 1024 * 1024; // (5 MB)
$filename = nuSanitizeFilename(basename($_FILES['file']['name']));
$target_dir = $_SERVER['DOCUMENT_ROOT']. '/forte/upload/files/';
$target_file = $target_dir . $filename;
$error = 'Error: ';

try {

$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
 if (!in_array($ext, $allowed)) {
  throw new Exception('File must be in CSV format');
 }

 $filesize = $_FILES["file"]["size"];
 if ($filesize > $maxfilesize) {
  throw new Exception('Maximum file size 5MB exceeded');
 }

 $cprid = isset($_POST["cpr_id"]) ? $_POST["cpr_id"] : '';
 if ($cprid == '') {
  throw new Exception('No card issuer selected');
 }

 if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
  $contents = file_get_contents($target_file);

...my file processing and data manipulation goes here...

  $data = ['url' => $target_file, 'file' => $filename, 'message' => "{$lines} transactions uploaded"];
  http_response_code(201);
  echo json_encode($data);
 } else {
  throw new Exception('Unable to move the uploaded file to its final location:' . $target_file);
 }
} catch (Exception $th) {
 $data = ['message' => $error, 'error' => $th->getMessage()];
 http_response_code(200);
 echo json_encode($data);
}
(I had another recent issue with Uppy whereby I had to change the http_response_code to 200 in the error section as a 400 would give a misleading error at the browser blaming a network issue!)

Neil.

Re: Uppy 401 Unauthorized

Posted: Fri May 23, 2025 4:44 pm
by kev1n
And with the default one, do you also get the error?

Re: Uppy 401 Unauthorized

Posted: Fri May 23, 2025 5:02 pm
by vario
Kevin,

You've got me there! To be honest I am in a little over my head with Uppy, so I have been working away at using the more basic code in your github repository. I might try to investigate but as it looks like some issue with my code or setup I will probably not have the time or energy to track it down. (sorry :? :( :roll: )

My solution (which I've tried to resurrect in one of my other posts ) is to have PHP on the server to move the uploaded file to a pre-defined path and then use nuRunPHPHidden after a successful upload in the HTML section of the upload button:

Code: Select all

var result = JSON.parse(data);
if (result.error) {
   nuMessage("Upload Failed: " + result.error);
} else {
   nuRunPHPHidden("php_loadTransactions"); // This will pass messages back via nuJavaScriptCallback()
   refreshiframe('ifr_up_browse');
}
Neil.