javascript - RNFetchBlob - 无法在生成的图像文件中获取 API 数据

标签 javascript react-native image rn-fetch-blob

大家好,我在我的网站上设计了证书图像模块,当我单击“下载”时,我能够生成包含所有数据的证书图像文件。 但现在,我使用“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

enter image description here App.js

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/

相关文章:

javascript - 如何将动态 html 添加到 jQuery 对话框的文本字段?

html - 共享链接时如何更改与网页关联的图像?

jquery - 预加载图像并在加载时显示微调器

react-native - 在 React Native 的原生基础库中更改 fontFamily 不起作用

javascript - React-native Redux 计数器示例 : explain about reducer

javascript - React Native borderRadius 一个 Angular 不圆

android - 可绘制不显示

javascript - BackboneJs教程问题

javascript - 如何使用 Javascript 输出带有变量插值的多行 HTML?

javascript - 使用 jquery 将 &lt;script&gt; 附加到 <div> 中