React native 需要能够在componentdidmount上下载给定BASE64字符串的PDF
我正在尝试为给定Base64字符串的PDF提供下载功能。我可以使用“react native view PDF”查看PDF。只是不知道如何下载文件。这需要在android和ios上运行 我尝试过各种论坛,但遗憾的是,我还是没有找到 注意:this.props.pdf是Base64字符串 尝试1) 尝试2)React native 需要能够在componentdidmount上下载给定BASE64字符串的PDF,react-native,react-native-android,react-native-ios,react-native-fetch-blob,React Native,React Native Android,React Native Ios,React Native Fetch Blob,我正在尝试为给定Base64字符串的PDF提供下载功能。我可以使用“react native view PDF”查看PDF。只是不知道如何下载文件。这需要在android和ios上运行 我尝试过各种论坛,但遗憾的是,我还是没有找到 注意:this.props.pdf是Base64字符串 尝试1) 尝试2) RNFetchBlob.config({ fileCache:true, 附录:“pdf” }) .fetch('GET',`${this.props.PDFLink}`) 。然后((res)
RNFetchBlob.config({
fileCache:true,
附录:“pdf”
})
.fetch('GET',`${this.props.PDFLink}`)
。然后((res)=>{
//直接打开文档
如果(Platform.OS==“ios”){
RNFetchBlob.ios.previewDocument(res.path())
}
否则{
RNFetchBlob
.config({
AddAndroid下载:{
useDownloadManager:true,//{
//下载文件的路径
响应路径()
})
}
})
.catch(错误=>{
控制台错误(error);
});
我希望看到当屏幕加载时,用户可以下载PDF。我已经向用户显示了它,只是希望他们也能够下载文件。要下载文件,可以使用RNFetchBlob.fs.writeFileapi从rn fetch blob下载文件,如下所示。您也可以参考其他文件从文档中提取流式API
RNFetchBlob
.config({
AddAndroid下载:{
useDownloadManager:true,//{
//下载文件的路径
//响应路径()
设base64Str=resp.data;
让pdfLocation=RNFetchBlob.fs.dirs.DocumentDir++'/'+'test.pdf';
RNFetchBlob.fs.writeFile(pdfLocation,RNFetchBlob.base64.encode(base64Str),'base64');
})
希望这有帮助:)Hi@Dinesh Nadimpalli,我的PDF文件没有存储在我们创建运行时base64并通过API发送的服务器上。因此,我有一个url返回base64字符串res.success.PDF,我如何使用它来下载PDF。
var path = RNFetchBlob.fs.dirs.DocumentDir + "/bill.pdf";
RNFetchBlob.fs.writeFile(path, this.props.pdf, "base64").then(res => {
console.log("File : ", res);
});
RNFetchBlob.config({
fileCache : true,
appendExt : 'pdf'
})
.fetch('GET',`${this.props.PDFLink}`)
.then((res) => {
// open the document directly
if(Platform.OS == "ios"){
RNFetchBlob.ios.previewDocument(res.path())
}
else{
RNFetchBlob
.config({
addAndroidDownloads : {
useDownloadManager : true, // <-- this is the only thing required
// Optional, override notification setting (default to true)
notification : false,
// Optional, but recommended since android DownloadManager will fail when
// the url does not contains a file extension, by default the mime type will be text/plain
mime : 'application/pdf',
description : 'File downloaded by download manager.'
}
})
.fetch('GET',`${this.props.PDFLink}`)
.then((resp) => {
// the path of downloaded file
resp.path()
})
}
})
.catch(error => {
console.error(error);
});
RNFetchBlob
.config({
addAndroidDownloads : {
useDownloadManager : true, // <-- this is the only thing required
// Optional, override notification setting (default to true)
notification : false,
// Optional, but recommended since android DownloadManager will fail when
// the url does not contains a file extension, by default the mime type will be text/plain
mime : 'application/pdf',
description : 'File downloaded by download manager.'
}
})
.fetch('GET',`${this.props.PDFLink}`)
.then((resp) => {
// the path of downloaded file
// resp.path()
let base64Str = resp.data;
let pdfLocation = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'test.pdf';
RNFetchBlob.fs.writeFile(pdfLocation, RNFetchBlob.base64.encode(base64Str), 'base64');
})