nolanlawson / blob-util

Cross-browser utils for working with binary Blobs
https://nolanlawson.github.io/blob-util
Apache License 2.0
503 stars 45 forks source link

Wrong characters when stream is text/plain #60

Closed darkman97i closed 4 years ago

darkman97i commented 4 years ago

Hi,

I'm getting wrong character when getting data from text/plain stream, this is my code:

return this._ws.requestBinaryStream(options).then(response => {
      let content = BlobUtil.arrayBufferToBinaryString(response.data);
      console.log(content);
      content = new TextDecoder('utf-8').decode(response.data);
      console.log(content);
      return content;
    });

This is my requestBinaryStream:

requestBinaryStream({ method, url, contentType, params, token }) {
    // console.log('Request: [', method, '] ', url);
    return axios({
      method,
      url: url,
      responseType: 'arraybuffer',
      data: JSON.stringify(params),
      headers: {
        'X-Requested-App': 'kcenter',
        ...(token && { Authorization: `OKM ${token}` })
      }
    })
      .then(response => {
        // const { status, data } = response;
        // console.log(`Response ${status} - ${JSON.stringify(data)}`);
        return response;
      })
      .catch(error => {
        throw this.buildException(error);
      });
  }

I success get working with TextDecoder. My question is if there's some function what can be used from your library either TextDecoder or if you can add similar support in future.

Thanks for your time.

nolanlawson commented 4 years ago

Hi, you may want to ask this question on StackOverflow rather than this repo. This repo doesn't really handle encodings at all; it just converts from one binary format to another. I'm not sure what's not working with your code, but a quick glance says it may be an issue with how you're requesting the data from the server or how it's encoded on the server side. Good luck!