Javascript 如何使用axios在react native中使用auth令牌调用直接pdf下载api
我正在调用一个api,它可以直接下载pdf,在postman中可以完美地工作,但在带有axios的react本机应用程序中无法工作 我也试着用blob格式调用,但是我得到了一个错误,无法为blob创建URL强>Javascript 如何使用axios在react native中使用auth令牌调用直接pdf下载api,javascript,react-native,react-native-android,react-native-ios,Javascript,React Native,React Native Android,React Native Ios,我正在调用一个api,它可以直接下载pdf,在postman中可以完美地工作,但在带有axios的react本机应用程序中无法工作 我也试着用blob格式调用,但是我得到了一个错误,无法为blob创建URL axios({ method: GET, url: url, responseType: 'blob', headers: { 'Content-type': 'application/pdf', "x
axios({
method: GET,
url: url,
responseType: 'blob',
headers: {
'Content-type': 'application/pdf',
"x-requested-with":"XMLHttpRequest",
Accept: "application/pdf",
Authorization: `Bearer ${token}`,
},
})
.then(response => {
const file = new Blob(
[response.data],
{type: 'application/pdf'});
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
.catch(err => {
isDev && console.log('error we got back', err);
});
如果没有blob格式,我将得到以下值作为响应:
"03123 00000 n
0000015836 00000 n
0000024222 00000 n
0000000289 00000 n
0000002463 00000 n
0000002483 00000 n
0000002529 00000 n
0000002581 00000 n
0000002633 00000 n
0000002956 00000 n
0000002820 00000 n
0000002697 00000 n
0000003019 00000 n
0000003468 00000 n
0000006174 00000 n
0000003244 00000 n
0000003448 00000 n
0000006195 00000 n
0000006455 00000 n
0000014634 00000 n
0000015056 00000 n
0000014613 00000 n
0000015972 00000 n
0000016237 00000 n
0000023223 00000 n
0000023575 00000 n
0000023202 00000 n
trailer
<<
/Size 32
/Info 1 0 R
/Root 17 0 R
>>
startxref
24461
%%EOF
“03123 00000n
000001583600000N
000002422200000N
000000028900000N
000000 24630万n
000000 2483 00000 n
000000 2529 00000新西兰元
000000 2581 00000 n
000000 26330 00000 n
000000 2956 00000 n
000000 2820 00000 n
000000269700000N
000000 3019 00000 n
000000346800000N
0000006174000000N
000000 3244 00000 n
000000344800000N
000000619500000N
000000 6455 00000 n
0000014634000N
000001505600000N
00000146130000N
0000015972000000N
0000016237000000N
000002322300000N
0000023575 00000N
0000023202 00000N
拖车
>
起始外部参照
24461
%%EOF
“您能否遵循此链接:已尝试,但得到相同的错误。
const {config, fs} = RNFetchBlob;
let DownloadDir = fs.dirs.DownloadDir;
let options = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
title: `${name} file report`,
path: DownloadDir + `/${name}Report` + '.pdf',
description: 'Downloading file.',
},
};
config(options)
.fetch(
"GET",
url,
{
Authorization: `Bearer ${access_token}`,
},
)
.then(response => {
console.log('The file saved to ', response .path());
})
.catch(err => {
console.log(' response in catch123', err);
return reject(err);
});