大家好,我在我的网站上设计了证书图像模块,当我单击“下载”时,我能够生成包含所有数据的证书图像文件。 但现在,我使用“rn-fetch-blob”中的 RNFetchBlob 为 React Native 设计相同的内容,当我调用 api 时,我能够生成图像,但无法获取我的姓名和城市,如其所示代码..
还有一件事,当我console.log时,我能够看到带有名称和城市的api数据
我猜问题是Remote_Image_Path
OnPress = 下载图片
控制台.log
https://example.com/xxxApi/xxxxpath?name=JasonBourne&city=NewYork&uniqueId=xx4434cc/xxx.png
const downloadImage = () => {
const REMOTE_IMAGE_PATH =
(`${API_URL}/xxxApi/xxxxpath?name=${apiData.name}&city=${apiData.city}&uniqueId=${route.params.id}`+"/xxx.png");
setImageUrl(REMOTE_IMAGE_PATH);
console.log(imageUrl);
// Main function to download the image
// To add the time suffix in filename
let date = new Date();
// Image URL which we want to download
let image_URL = (`${API_URL}/puxxxApiblicApi/xxxxpath?name=${apiData.name}&city=${apiData.city}&uniqueId=${route.params.id}`+"/xxx.png")
// Getting the extention of the file
let ext = getExtention(imageUrl);
ext = '.' + ext[0];
// Get config and fs from RNFetchBlob
// config: To pass the downloading related options
// fs: Directory path where we want our image to download
const { config, fs } = RNFetchBlob;
let PictureDir = fs.dirs.PictureDir;
let options = {
fileCache: true,
addAndroidDownloads: {
// Related to the Android only
useDownloadManager: true,
notification: true,
path:
PictureDir +
'/xxpicturepath' +
Math.floor(date.getTime() + date.getSeconds() / 2) +
ext,
description: 'Image',
},
};
config(options)
.fetch('GET', imageUrl)
.then(res => {
// Showing alert after successful downloading
console.log('res -> ', JSON.stringify(res));
alert('Image Downloaded Successfully.');
});
};
最佳答案
刚刚在uniqueId=${route.params.id}后添加了&x,用于分隔带有扩展名的链接
const downloadImage = () => {
const REMOTE_IMAGE_PATH =
(`${API_URL}/xxxApi/xxxxpath?name=${apiData.name}&city=${apiData.city}&uniqueId=${route.params.id}&x`+"/xxx.png");
// Main function to download the image
// To add the time suffix in filename
let date = new Date();
// Image URL which we want to download
let image_URL = REMOTE_IMAGE_PATH;
// Getting the extention of the file
let ext = getExtention(imageUrl);
ext = '.' + ext[0];
// Get config and fs from RNFetchBlob
// config: To pass the downloading related options
// fs: Directory path where we want our image to download
const { config, fs } = RNFetchBlob;
let PictureDir = fs.dirs.PictureDir;
let options = {
fileCache: true,
addAndroidDownloads: {
// Related to the Android only
// xxpicturepath is a directory name in android storage for storing image
useDownloadManager: true,
notification: true,
path:
PictureDir +
'/xxpicturepath' +
Math.floor(date.getTime() + date.getSeconds() / 2) +
ext,
description: 'Image',
},
};
config(options)
.fetch('GET', imageUrl)
.then(res => {
// Showing alert after successful downloading
console.log('res -> ', JSON.stringify(res));
alert('Image Downloaded Successfully.');
});
};
关于javascript - RNFetchBlob - 无法在生成的图像文件中获取 API 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74098069/