Javascript 如何在React中引用pdf文件
我有一个Reactjs SPA,其文件结构如下所示:Javascript 如何在React中引用pdf文件,javascript,reactjs,single-page-application,filepath,Javascript,Reactjs,Single Page Application,Filepath,我有一个Reactjs SPA,其文件结构如下所示: import React, {Component} from 'react'; class Resume extends Component { constructor(props) { super(props); this.state = {}; } render() { return ( <div className={'resum
import React, {Component} from 'react';
class Resume extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div className={'resume-container'}>
<div id={'resume-pdf'}>You can view, download and print my résumé in pdf format in <a
href={'./public/resume_english.pdf'}>English</a> and <a href={'#'}>Spanish</a></div>
</div>
);
}
}
export default Resume;
在该应用程序中有一个简单的组件(Resume.js),我希望用户能够在浏览器中查看/下载pdf文件(Resume\u english.pdf)。然而,无论是在本地还是在我计划托管SPA的GitHub页面上,我都无法做到这一点。它总是将我重定向到主页
Resume.js如下所示:
import React, {Component} from 'react';
class Resume extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div className={'resume-container'}>
<div id={'resume-pdf'}>You can view, download and print my résumé in pdf format in <a
href={'./public/resume_english.pdf'}>English</a> and <a href={'#'}>Spanish</a></div>
</div>
);
}
}
export default Resume;
import React,{Component}来自'React';
类恢复扩展组件{
建造师(道具){
超级(道具);
this.state={};
}
render(){
返回(
您可以在和中查看、下载和打印我的pdf格式简历
);
}
}
导出默认恢复;
有什么想法可以解决问题吗?我已经尝试导入了,但没有成功。看起来这是用create react应用程序创建的。如果是这样,则
public
目录中的文件将复制到build
,而build
目录将用作站点的根目录。因此,合适的href是/resume\u english.pdf
只需直接在浏览器中输入url即可。类似于
http://localhost:3000/resume_english.pdf
或https://danielsto.github.io/resume_english.pdf
看起来这是用CreateReact应用程序制作的。如果是这样,则public
目录中的文件将复制到build
,而build
目录将用作站点的根目录。因此,合适的href是/resume\u english.pdf
只需直接在浏览器中输入url即可。类似于
http://localhost:3000/resume_english.pdf
或https://danielsto.github.io/resume_english.pdf
它可以在本地工作,但不能在GitHub页面上工作。拥有href={/resume\u english.pdf}
会导致https://danielsto.github.io/resume_english.pdf
,即未找到站点
。存储库是私有的,这可能是问题的一部分吗?取决于github页面repo的配置方式。如果是项目页面(danielsto.github.io/reponame),而不是用户页面(danielsto.github.io),则可以使用相对url/resume\u english.pdf
,它可以在本地工作,但不能在github页面上工作。拥有href={/resume\u english.pdf}
会导致https://danielsto.github.io/resume_english.pdf
,即未找到站点
。存储库是私有的,这可能是问题的一部分吗?取决于github页面repo的配置方式。如果是项目页面(danielsto.github.io/reponame),而不是用户页面(danielsto.github.io),则可以使用相对url/resume\u english.pdf