There's a small assumption developers sometimes make when building file-upload interfaces:
If the filename ends in .pdf, it must be a PDF.
Convenient assumption.
Unfortunately, files don't particularly care about our naming conventions.
A user can rename an image to document.pdf, select a malformed document or provide a file that your processing library simply cannot understand.
The extension is useful, but it isn't validation.
After working on browser-based file utilities, I've come to appreciate how important this distinction is.
Here are 5 things I'd consider when building a file-processing workflow.
- File extensions are only an initial filter
Checking a filename is useful for improving the user experience.
For example:
function hasPdfExtension(file) {
return file.name.toLowerCase().endsWith(".pdf");
}
This can help filter files before processing.
But it doesn't establish that the contents are a valid PDF.
Someone can rename image.png to image.pdf in a few seconds.
Your application should treat the extension as a hint, not proof.
- MIME types aren't a perfect solution either
You might decide to check:
if (file.type !== "application/pdf") {
throw new Error("Unsupported file type");
}
That's another useful check, but it has limitations.
The browser-provided MIME type can be empty, inconsistent or based on information that doesn't reliably establish the actual file structure.
Different environments can also behave differently.
Using the extension and MIME type together improves preliminary filtering, but neither should replace parsing the actual file.
- Let the parser verify the structure
For a PDF utility, the next step should be attempting to parse the document using a suitable PDF library.
For example, PDF.js can attempt to load a document:
const loadingTask = pdfjsLib.getDocument({
data: await file.arrayBuffer()
});
const pdf = await loadingTask.promise;
console.log("Pages:", pdf.numPages);
If parsing fails, your application should handle that failure gracefully.
A successful parse doesn't mean every possible operation will work, either.
A document can contain unusual structures or features that aren't supported by your chosen library.
Validation is an ongoing process, not one checkbox.
- File size and resource limits are part of validation
A technically valid file can still be too large for your application to process comfortably.
Consider two PDFs:
One contains 10 pages of ordinary text.
Another contains 200 high-resolution scanned pages.
Their filenames and extensions might be identical.
Their processing requirements certainly aren't.
Check file size before allocating large processing resources.
Where possible, also consider page counts, image dimensions and the specific operation being performed.
Avoid promising that a particular maximum file size will work universally. Available browser memory varies considerably between devices.
- Design useful errors instead of generic failures
This is where many file utilities become frustrating.
The user selects a file, clicks Process, waits, and eventually gets:
Something went wrong.
Wonderful. Very informative.
I'd separate errors into meaningful categories:
Unsupported file type.
Invalid or malformed document.
Unsupported document feature.
File exceeds practical processing limits.
Processing failed unexpectedly.
The user doesn't need a stack trace.
They need to understand what happened and what they can try next.
Also, don't expose sensitive internal details in user-facing error messages.
A small distinction that changes the whole workflow
File selection, preliminary checks, actual parsing and operation-specific validation should be treated as separate stages.
That makes it easier to identify where something failed.
It also makes testing considerably more useful because you're not treating every unsuccessful operation as the same problem.
For anyone building browser-based file utilities, I'd recommend testing renamed files, malformed inputs, empty files and unusually large documents before considering the workflow finished.
How do you validate user-selected files in your projects?
Top comments (0)