【问题标题】:React-Native Base 64 Data to FileSystem issueReact-Native Base 64 数据到文件系统问题
【发布时间】:2021-06-27 15:24:58
【问题描述】:

我有一个非常简单的 PHP 文件,它将我的图像作为 BASE64 数据返回

<?php
require_once 'database_connections.php'; 
$data = json_decode(file_get_contents("php://input"));

$id = mysqli_real_escape_string($con, $data->id);
$page = mysqli_real_escape_string($con, $data->page);

$path = "../../images/" . $id . '/' . $page . '.jpg';

$imagedata = file_get_contents($path);
$base64 = base64_encode($imagedata);   

echo($base64);

?>

在我的反应应用程序中,我正在获取如下数据

export const getImage = async () => {
  try {
    const response = await axios.post(
      url,
      {
        id: '11bb2c1b-c262-4171-b614-d8af46898efb',
        page: '001',
      }
    );
    return response.data;
  } catch (error) {
    // handle error
    console.error(error.message);
  }
};

我的 base64 响应如下:

/9j/2wCEAAEBAQEBAQEBAQECAQEBAgICAQECAgICAgICAgIDAgMDAw...

最后我的文件系统代码如下:

const image = await getImage();
  const filename = FileSystem.documentDirectory + 'imagetest.jpg';
  await FileSystem.writeAsStringAsync(filename, image, {
    encoding: FileSystem.EncodingType.Base64,
  });

我没有收到任何错误,但在我尝试时我的文件没有被保存。我已经为此工作了几个小时,但没有解决我的问题。

任何建议将不胜感激!

编辑:我可以在 Image 组件中显示 base64 图像。

【问题讨论】:

  • 你能把你的base64图像从react-native显示到Image吗?
  • @VasylNahuliak 感谢您的评论。是的,我刚刚尝试过,我可以将图像显示到 Image 组件中。

标签: php react-native base64 filesystems


【解决方案1】:

您将无法在 FileSystem.documentDirectory 中看到您保存的图像文件,您需要

FileSystem.readDirectoryAsync(FileSystem.documentDirectory).then(data => {
data.forEach(filename_ => {
  console.log("=cached image==***===" + filename_)
})

检查它是否存在。 FileSystem.documentDirectory 只是“实用可访问”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多