为了下载资产,在结合使用react-native-fs和React Native时,需要设置Authorization头。
按照文档中的说明,将标头设置如下:
const options = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
fromUrl: url,
toFile: path,
};
// const permission = await insurePermissions();
const task = RNFS.downloadFile(options);它在iOS中运行得很好,但是安卓使用的是运行安卓6、8、9或10的模拟器,报头没有被发送,所以服务器返回了一个错误的不同资产,因为用户没有被验证。
Android中的Authentication头是如何通过react-native-fs设置的?
发布于 2019-11-13 07:59:03
这个问题与其他React Native文件系统库一致。我转而使用Axios下载该文件,并用RNFS保存。
const fetchFile = async (accessToken, id) => {
const dir = await getDirectory();
await flushFileProofs();
const baseUrl = 'https://example.com';
const url = `${baseUrl}/images/${id}/download`;
const fileName = `${id}-file.png`;
const path = `${dir}/${fileName}`;
const data = await axios.request({
method: 'GET',
url,
headers: {
Accept: '*/*',
Authorization: `Bearer ${accessToken}`,
},
responseType: 'arraybuffer',
})
.then(response => Buffer.from(response.data, 'binary').toString('base64'));
await RNFS.writeFile(path, data, 'base64');
FileViewer.open(path);
};https://stackoverflow.com/questions/58685059
复制相似问题