{"record":{"id":"d680091e1f7e7e95","repo":"naptha/tesseract.js","slug":"file-could-not-be-read-code-code","errorCode":null,"errorMessage":"File could not be read! Code=${code}","messagePattern":"File could not be read! Code=(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/worker/browser/loadImage.js","lineNumber":17,"sourceCode":"'use strict';\n\n/**\n * readFromBlobOrFile\n *\n * @name readFromBlobOrFile\n * @function\n * @access private\n */\nconst readFromBlobOrFile = (blob) => (\n  new Promise((resolve, reject) => {\n    const fileReader = new FileReader();\n    fileReader.onload = () => {\n      resolve(fileReader.result);\n    };\n    fileReader.onerror = ({ target: { error: { code } } }) => {\n      reject(Error(`File could not be read! Code=${code}`));\n    };\n    fileReader.readAsArrayBuffer(blob);\n  })\n);\n\n/**\n * loadImage\n *\n * @name loadImage\n * @function load image from different source\n * @access private\n */\nconst loadImage = async (image) => {\n  let data = image;\n  if (typeof image === 'undefined') {\n    return 'undefined';\n  }\n","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/naptha/tesseract.js/blob/a1ca80d9e31c34512d0ded75ff8821ddcf3f2f91/src/worker/browser/loadImage.js#L1-L35","documentation":"In the browser worker's loadImage, File/Blob/Canvas inputs flow through readFromBlobOrFile, which uses FileReader.readAsArrayBuffer. If the reader's onerror fires, the promise rejects with the FileReader error code. Standard DOM error codes: 1=NOT_FOUND_ERR, 2=SECURITY_ERR, 3=ABORT_ERR, 4=NOT_READABLE_ERR, 12=SYNTAX_ERR, 13=ENCODING_ERR.","triggerScenarios":"Passing a File whose underlying file was moved/deleted after selection; a Blob backed by a revoked object URL; a cross-origin-tainted canvas; a file the OS refuses to read (permissions/lock); an aborted read.","commonSituations":"User selects a file in an <input type=file> then the file disappears; URL.createObjectURL blob revoked too early; canvas drawn with cross-origin images without crossOrigin='anonymous'; security policy in the worker context blocking file reads.","solutions":["Map the code: 1 -> file missing, 2 -> security/tainted, 4 -> permissions/lock; act accordingly (re-prompt, fix CORS, release lock).","Do not call URL.revokeObjectURL on a blob URL before passing the blob to the worker.","For cross-origin canvas sources, set img.crossOrigin = 'anonymous' and serve images with proper CORS headers.","Validate the File/Blob is non-empty and still readable (size > 0, type set) before handing it to recognize/detect."],"exampleFix":"// before\nconst url = URL.createObjectURL(blob);\nURL.revokeObjectURL(url); // revoked too early\nawait worker.recognize(blob); // code 4 / security\n\n// after\nconst url = URL.createObjectURL(blob);\nawait worker.recognize(blob);\nURL.revokeObjectURL(url); // revoke only after recognize resolves","handlingStrategy":"validation","validationCode":"// Validate File/Blob is non-empty and still readable before handing it to the worker.\nfunction assertReadableBlob(blob) {\n  if (!(blob instanceof Blob)) throw new Error('Expected Blob/File');\n  if (blob.size === 0) throw new Error('Blob is empty');\n  if (blob.isClosed) throw new Error('Blob is closed');\n}\nassertReadableBlob(file);\nawait worker.recognize(file);","typeGuard":"const isReadableFile = (f) =>\n  (f instanceof File || f instanceof Blob) &&\n  f.size > 0 &&\n  !f.isClosed;","tryCatchPattern":"const FILE_ERROR_CODES = { 1: 'NOT_FOUND', 2: 'SECURITY', 3: 'ABORT', 4: 'NOT_READABLE' };\ntry {\n  await worker.recognize(file);\n} catch (e) {\n  const m = e.message.match(/Code=(\\d+)/);\n  if (m && FILE_ERROR_CODES[m[1]] === 'NOT_READABLE') {\n    // prompt user to re-select the file, then retry\n    return;\n  }\n  throw e;\n}","preventionTips":["Do not call URL.revokeObjectURL on a blob URL until after recognize/detect resolves.","For cross-origin canvas sources, set img.crossOrigin = 'anonymous' and require CORS headers.","When accepting user-selected files, copy/snapshot the bytes immediately rather than holding the File handle.","Map the FileReader error code to a cause (1 missing, 2 security, 4 unreadable) before retrying."],"tags":["browser","filereader","input-validation","file","security"],"backgroundTag":null,"analyzedSha":"a1ca80d9e31c34512d0ded75ff8821ddcf3f2f91","analyzedAt":"2026-08-13T04:28:13.744Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}