openks / learn-vue

自定义组件文档
https://openks.github.io/learn-vue
0 stars 0 forks source link

20180503_react使用fetch获取图片的base64无法正常解析问题 #106

Open openks opened 6 years ago

openks commented 6 years ago
// 发送ajax请求返回base64类型的字符串
// 直接复制network的response里的字符串作为src图片是可以正常显示的
fetch('/imageCode', {
    method: 'GET',
    mode: 'cors',
}).then(function(response) {
  // response.type "basic"
  // response.body:ReadableByteStream
  console.log('response.data', response)
});
openks commented 6 years ago

then里做如下处理是可以的

    let reader = response.body.getReader();
    let decoder = new TextDecoder();
    let str=''
    reader.read().then(function(result) {
      str=decoder.decode(result.value)
      _this.setState({'imgsrc': str});
    })
openks commented 6 years ago

处理基本的Text/HTML响应

fetch('/imageCode', {
    method: 'GET',
    mode: 'cors',
})
.then((response) => response.text())
.then((txt)=>{
    console.log(txt)
})