我有一个非常简单的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);
>
在我的react应用程序中,我像下面的一样获取这些数据
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...
最后我的FileSystem代码如下:
const image = await getImage();
const filename = FileSystem.documentDirectory + 'imagetest.jpg';
await FileSystem.writeAsStringAsync(filename, image, {
encoding: FileSystem.EncodingType.Base64,
});
我没有收到任何错误,但我的文件在尝试时没有被保存。我已经为此工作了几个小时,但没有解决我的问题。
任何建议都将不胜感激!
编辑:我可以在图像组件中显示base64图像。
您将无法在FileSystem.documentDirectory中看到您保存的图像文件,您需要
FileSystem.readDirectoryAsync(FileSystem.documentDirectory).then(data => {
data.forEach(filename_ => {
console.log("=cached image==***===" + filename_)
})
检查它是否在那里。FileSystem.documentDirectory仅为";实用可访问";