2017-10-11 181 views
0

我正在迁移一个反应/ php项目(它运行在apache上)创建反应应用程序,所以Express.js /活重装等(用于开发) 。通过php文件动态创建反应应用程序和加载图像

在这个项目中有从用户上传的图像,它们通过php文件保存在文档根目录之外,接受查询字符串作为要加载的文件的名称。

file.php?image=file_name.png 

及其相关反应的成分是

const Img = (props) => { 

    const findFile =() => { 
     // base64 
     if(props.file && props.file.startsWith('data:image/')){ 
      return props.file; 
     } 

     return "file.php?name=" + props.file + "&type=" + props.type; 
    }; 

    return (
     <img src={findFile()} alt={props.alt || props.file}/> 
    ); 

}; 

迁移到创造 - 反应 - 应用程序后,我的package.json

"proxy":"http://localhost" 

,但这样一来,增加了代理仅当用户在仪表板页面上时加载图像。在其他页面中,图像不起作用,因为更改了file.php的路径。

在仪表板(HTTP:/本地主机/ AUTH)

127.0.0.1 - - [10/Oct/2017:23:59:12 +0200] "GET /file.php?name=avatar4.png&type=avatarDefaultDir HTTP/1.1" 200 12295 

在其他页面(如:http://localhost/auth/profile

127.0.0.1 - - [10/Oct/2017:23:59:17 +0200] "GET /auth/file.php?name=avatar4.png&type=avatarDefaultDir HTTP/1.1" 200 27 

我怎么能解决这个问题?

+0

使用绝对网址 – madalinivascu

回答

0

您需要使用正确的路径file.php文件,并使用绝对路径file.php

,而不是

return "file.php?name=" + props.file + "&type=" + props.type; 

使用

return "/file.php?name=" + props.file + "&type=" + props.type; 

这是假设file.php位于您项目的根目录

相关问题