Skip to content

readBlobAsText does not repect the reponse charset #1059

@joe06102

Description

@joe06102

I'm working on a react-native project which is dependent of this lib. But I found the readBlobAsText does not respect the charset in Content-Type, which throws error when the charset is not utf8-encoded.

the problematic code below:

function readBlobAsText(blob) {
  var reader = new FileReader()
  var promise = fileReaderReady(reader)
  reader.readAsText(blob) // <-- FilerReader accept a second param 'encode', which can be retrieved from the response header.
  return promise
}

And here is my code suggestion:

  function getEncode() {
    const contentType = xhr.getResponseHeader('content-type')
    return /charset=([A-Za-z0-9_\-]+)/.exec(contentType)?.[1] || 'utf-8'
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions