我将 apisauce 用于基本的 http 请求,如 GET 和 POST,并且效果很好。现在我也需要能够下载 pdf 文件。用apisauce可以吗?如果是这样,有人有一些示例代码吗?
user15707069
问问题
110 次
1 回答
0
我不确定apisauce,但对于从互联网上的url下载pdf,
我更喜欢使用这个包: rn-fetch-blob作为它的稳定包并且有很好的文档。
按着这些次序:
- 在终端(MAC/Linux)或命令提示符(Windows)中运行以下命令
纱线添加 rn-fetch-blob
- 如果使用 CocoaPods,请将 pod 添加到您的 Podfile
pod 'rn-fetch-blob', :path => '../node_modules/rn-fetch-blob'
- 在.js或.tsx文件中导入包
- 在下载函数中添加以下代码并使用下载按钮调用该函数
pressed
if (Platform.OS === 'android') ==>
RNFetchBlob.config({
fileCache: false,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
mime: 'application/pdf',
path:
`/storage/emulated/0/Download/${file_name}.pdf`,
},
})
.fetch('GET', api, {'Cache-Control': 'no-store'})
.then((res) => {})
.catch((errorMessage, statusCode) => {});
如果(Platform.OS === 'ios')
const {
dirs: {DownloadDir, DocumentDir},
} = RNFetchBlob.fs;
const fPath = `${DocumentDir}/${file_name}.pdf`;
RNFetchBlob.config({
fileCache: true,
path: fPath,
notification: true,
appendExt: 'pdf',
})
.fetch('GET', api, {
Authorization: 'Token ' + token,
})
.then(async (resp) => {
RNFetchBlob.fs.writeFile(fPath, resp.data, 'base64');
RNFetchBlob.ios.previewDocument(fPath);
});
于 2021-05-07T18:50:22.053 回答