From e2b14ecf1099f4bae17c36d2ec37d4fe0feeae0b Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sun, 9 Mar 2025 21:04:15 -0500 Subject: [PATCH] receipt-form: Omit empty uploaded file If there is no file to be uploaded, then we must not send the value of the `photo` file input. Doing so causes two files to be uploaded, the first one being a zero-byte file with no name, and the second one being the one we captured from the camera. The server only uses the first uploaded file if there are multiple, so the correct file is never used. --- js/receipt-form.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/js/receipt-form.ts b/js/receipt-form.ts index 6ce5a9e..f208287 100644 --- a/js/receipt-form.ts +++ b/js/receipt-form.ts @@ -26,9 +26,12 @@ form.addEventListener("submit", async (evt) => { evt.preventDefault(); btnSubmit.loading = true; const data = new FormData(form); - const blob = await cameraInput.getBlob(); - if (blob) { - data.append("photo", blob, "photo.jpg"); + if (!inpImage.files?.length) { + data.delete("photo"); + const blob = await cameraInput.getBlob(); + if (blob) { + data.append("photo", blob, "photo.jpg"); + } } let r: Response; try {