Binary Files Corrupted During Upload or Processing
Files uploaded to or processed by your application come out corrupted. PDFs are unreadable, images show as broken, ZIP archives can't be extracted, and Excel files fail to open. The files appear to be the right size but their contents are garbled.
Binary file corruption typically happens when the application treats binary data as text (applying encoding transformations that destroy the data), when streams are not properly piped, or when files are read and written with mismatched encoding settings.
This issue is particularly insidious because it may only affect certain file types or file sizes, making it hard to reproduce consistently during testing.
Error Messages You Might See
Common Causes
- UTF-8 encoding applied to binary data — Reading binary files with encoding: 'utf-8' instead of as a Buffer or bytes object
- Base64 double-encoding — File data is base64 encoded during upload and encoded again during storage, doubling the corruption
- Multipart parsing misconfigured — The file upload middleware converts binary data to strings instead of preserving raw buffers
- Stream not set to binary mode — HTTP response or file stream uses text mode instead of binary mode
- Chunked transfer truncation — Large files are cut off because the receiving end closes the connection before all chunks arrive
How to Fix It
- Always use binary mode for file operations — Use fs.readFile without encoding option (returns Buffer) or open files with 'rb'/'wb' in Python
- Verify multipart parsing configuration — Ensure multer, busboy, or your upload library preserves raw binary buffers
- Test with actual binary files — Upload a real PDF, image, and ZIP file and verify they open correctly after download. Compare checksums
- Use streams correctly — Pipe readable streams directly to writable streams without intermediate string conversion
- Check Content-Type headers — Ensure the response sets application/octet-stream or the correct MIME type, not text/plain
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
Why are my uploaded files corrupted?
The most common cause is reading binary files as text (UTF-8). Ensure your upload handler preserves raw binary buffers. In Node.js, don't pass an encoding option to fs.readFile for binary files.
How do I verify file integrity after upload?
Compare MD5 or SHA256 checksums of the original file and the stored file. If they differ, the data was modified during transit or storage. Use crypto.createHash in Node.js or hashlib in Python.