FileReader returns garbage after a certain number of files in mobile safari

Been really stumped by this problem. I’ve searched the web and haven’t found traces of info on it, but I believe it is a widespread issue.

See the HTML in this post. If I select more than 350 (the bounding value works for my iPhone, but it may vary based on device) photos the first 350 will upload just fine. For photos after 350 each one will fail. When this fails, the data returned is what appears to be an empty plist and the FileReader doesn’t error. Here is a sample of the bytes returned when it fails (appears to be an empty plist):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>X$versionY$archiverT$topX$objects��_NSKeyedArchiver� Troot��U$null$)27ILQSU["
</code>
<code>X$versionY$archiverT$topX$objects��_NSKeyedArchiver� Troot��U$null$)27ILQSU[" </code>
X$versionY$archiverT$topX$objects��_NSKeyedArchiver�    Troot��U$null$)27ILQSU["

So, the question is: Is this a known issue I just haven’t found Apple’s documentation for? Is there a way to work around this without putting an arbitrary limit on the number of files you can select?

The HTML is below. I hosted it somewhere and debugged from my iPhone tethered to my Mac. I am looking at the image header to avoid dumping out more bytes than necessary and overloading the debugger console. I only log the contents of the first failure, but believe that all failure have the same contents.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><html>
<head>
<title>Tester</title>
</head>
<body>
<button id="selectButton">Select Photos</button>
<input type="file" id="fileInput" style="display: none;" accept="image/*" multiple>
<script>
document.getElementById('selectButton').addEventListener('click', function() {
document.getElementById('fileInput').click();
});
document.getElementById('fileInput').addEventListener('change', function(event) {
const files = event.target.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
testImage(file);
}
});
var savedOutput = false;
function testImage(file) {
var _reader = new window.FileReader(),
_fn = file.slice || file.mozSlice || file.webkitSlice || function(){return;},
_slice = _fn.call(file, 0, 3);
_reader.onloadend = function(_evt) {
var _isValid = false,
_bytes;
if (!_evt.currentTarget.error) {
_bytes = new window.Uint8Array(_evt.currentTarget.result);
if (
(_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) || //Check for jpg
(_bytes[0] == 0x42 && _bytes[1] == 0x4D) || //Check for BMP
(_bytes[0] == 0x89 && _bytes[1] == 0x50 && _bytes[2] == 0x4E) //Check for png
) {
_isValid = true;
} else {
if (_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) {
_isValid = true;
}
}
if(_bytes[0] == 0x00 && _bytes[1] == 0x00 && _bytes[2] == 0x00) { //Check for heic
_isValid = true;
}
}
console.log('File: ' + file.name + ' is a JPEG: ' + _isValid);
if (savedOutput === false && _isValid === false) {
savedOutput = true;
const decoder = new TextDecoder();
const text = decoder.decode(_bytes);
console.log('Failed Output: ', text);
}
}
_reader.readAsArrayBuffer(file);
}
</script>
</body>
</html>
I have tried this from more involved applications and it always fails. The failure seems related to file access.
</code>
<code><html> <head> <title>Tester</title> </head> <body> <button id="selectButton">Select Photos</button> <input type="file" id="fileInput" style="display: none;" accept="image/*" multiple> <script> document.getElementById('selectButton').addEventListener('click', function() { document.getElementById('fileInput').click(); }); document.getElementById('fileInput').addEventListener('change', function(event) { const files = event.target.files; for (let i = 0; i < files.length; i++) { const file = files[i]; testImage(file); } }); var savedOutput = false; function testImage(file) { var _reader = new window.FileReader(), _fn = file.slice || file.mozSlice || file.webkitSlice || function(){return;}, _slice = _fn.call(file, 0, 3); _reader.onloadend = function(_evt) { var _isValid = false, _bytes; if (!_evt.currentTarget.error) { _bytes = new window.Uint8Array(_evt.currentTarget.result); if ( (_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) || //Check for jpg (_bytes[0] == 0x42 && _bytes[1] == 0x4D) || //Check for BMP (_bytes[0] == 0x89 && _bytes[1] == 0x50 && _bytes[2] == 0x4E) //Check for png ) { _isValid = true; } else { if (_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) { _isValid = true; } } if(_bytes[0] == 0x00 && _bytes[1] == 0x00 && _bytes[2] == 0x00) { //Check for heic _isValid = true; } } console.log('File: ' + file.name + ' is a JPEG: ' + _isValid); if (savedOutput === false && _isValid === false) { savedOutput = true; const decoder = new TextDecoder(); const text = decoder.decode(_bytes); console.log('Failed Output: ', text); } } _reader.readAsArrayBuffer(file); } </script> </body> </html> I have tried this from more involved applications and it always fails. The failure seems related to file access. </code>
<html>
<head>
    <title>Tester</title>
</head>
<body>
    <button id="selectButton">Select Photos</button>
    <input type="file" id="fileInput" style="display: none;" accept="image/*"  multiple>
    <script>
        document.getElementById('selectButton').addEventListener('click', function() {
            document.getElementById('fileInput').click();
        });

        document.getElementById('fileInput').addEventListener('change', function(event) {
            const files = event.target.files;
            for (let i = 0; i < files.length; i++) {
                const file = files[i];
                testImage(file);
            }
        });
    
        var savedOutput = false;

        function testImage(file) {
            var _reader = new window.FileReader(),
                _fn = file.slice || file.mozSlice || file.webkitSlice || function(){return;},
                _slice = _fn.call(file, 0, 3);

                _reader.onloadend = function(_evt) {
                        var _isValid = false,
                            _bytes;
                        if (!_evt.currentTarget.error) {
                            _bytes = new window.Uint8Array(_evt.currentTarget.result);

                            if (
                                    (_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) ||    //Check for jpg
                                    (_bytes[0] == 0x42 && _bytes[1] == 0x4D) ||    //Check for BMP
                                    (_bytes[0] == 0x89 && _bytes[1] == 0x50 && _bytes[2] == 0x4E)    //Check for png
                                ) {
                                _isValid = true;
                            } else {
                                if (_bytes[0] == 0xFF && _bytes[1] == 0xD8 && _bytes[2] == 0xFF) {
                                    _isValid = true;
                                }
                            }

                            if(_bytes[0] == 0x00 && _bytes[1] == 0x00 && _bytes[2] == 0x00) {  //Check for heic
                                _isValid = true;
                            }
                        }

                        console.log('File: ' + file.name + ' is a JPEG: ' + _isValid);

                        if (savedOutput === false && _isValid === false) {
                            savedOutput = true;

                            const decoder = new TextDecoder();
                            const text = decoder.decode(_bytes);
                            console.log('Failed Output: ', text);
                        }
                }

                _reader.readAsArrayBuffer(file); 
            
        }
    </script>
</body>
</html>

I have tried this from more involved applications and it always fails.  The failure seems related to file access.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật