相同的网址将返回不同的图片(随机),并且我需要获取响应的标头(每次也会不同),因此我无法提取两次。

我尝试使用blob,但是得到一个警告,说'blob' is undefined,如下代码:

let response = await fetch(URLs.host + URLs.imageCode);
let key = response.headers.get('key');
console.log(response.blob); // this will print 'undefined'
let blob = await response.blob();
this.setState({source: URL.createObjectURL(blob)});

...

<Image source={{uri: this.state.source}} />


那么在加载图像时如何获取标题呢?

最佳答案

您不能使用fetch在每个this的React-Native中获取blob,因为fetch是一个本地http调用,它以本地级别存储blob,并且不提供对JavaScript blob对象中实际内存的引用。 res.blob()

除非有人为此写桥,否则最好的选择是使用base64编码的图像,该图像只是一个字符串,因此可以使用res.text()

关于javascript - 在 native 加载图像时如何获取响应头?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35538293/

10-12 00:08
查看更多