cordova-ios icon indicating copy to clipboard operation
cordova-ios copied to clipboard

Any possible reason why files in www folder load ok in emulator but not in device?

Open jfoclpf opened this issue 2 years ago • 2 comments

The files seem to load correctly from www folder when the app is run in emulator, but not when run from the device (iPhone SE 1gen).

Any possible hint why this is happening?

I am reading like this

  readFile(cordova.file.applicationDirectory + 'www/json/anomalies.json').then((res) => {
    const data = JSON.parse(res)

where readFile is

export function readFile (path, options) {
  options = options || {}

  return new Promise(function (resolve, reject) {
    window.resolveLocalFileSystemURL(path, (fileEntry) => {
      fileEntry.file((file) => {
        const reader = new FileReader()
        reader.onloadend = () => {
          resolve(reader.result)
        }

        const format = options.format
        switch (format) {
          case 'arrayBuffer':
            reader.readAsArrayBuffer(file)
            break
          case 'binaryString':
            reader.readAsBinaryString(file)
            break
          case 'dataURL':
            reader.readAsDataURL(file)
            break
          default:
            reader.readAsText(file)
        }

        reader.onerror = () => {
          reject(Error(`Error occurred reading file: ${path}`))
        }
      })
    }, (error) => {
      reject(Error('resolve error', error))
    })
  })
}

IMG_20230227_211127

Any help will be highly appreciated Thanks

jfoclpf avatar Feb 27 '23 21:02 jfoclpf

If it's erroring at the FileReader level, you may not be seeing the result because you're only hooking the onerror callback after the readAs* call.

Otherwise it will be good to include the error, if any.

I don't think it's related, but you resolve during onloadend but also reject in onerror. This is a problem because on error, both will be called and you are going to try to resolve or reject a a promise that already has an resolution. Instead of onloadend, you can use onload instead which will only fire on successful, which should guarantee that you resolve/reject the promise only once.

Otherwise, I don't see any a problem code wise.

breautek avatar Feb 27 '23 22:02 breautek

Thank you very much @breautek

If it's erroring at the FileReader level, you may not be seeing the result because you're only hooking the onerror callback after the readAs* call.

Should I then declare the reader.onerror before the readAs* call? Interesting, I will do it

I don't think it's related, but you resolve during onloadend but also reject in onerror. This is a problem because on error, both will be called and you are going to try to resolve or reject a a promise that already has an resolution. Instead of onloadend, you can use onload instead which will only fire on successful, which should guarantee that you resolve/reject the promise only once.

Thank you very much, it makes sense, I will amend onloadend to onload

jfoclpf avatar Feb 28 '23 15:02 jfoclpf

It seems that the discussion has reached a resolution. If the issue has been resolved, could you please close the ticket?

erisu avatar Mar 07 '24 10:03 erisu

thanks for all the support and great work

jfoclpf avatar Mar 07 '24 10:03 jfoclpf