【发布时间】:2020-06-15 02:31:36
【问题描述】:
我正在尝试使用用户界面上的HTML 链接从外部数据库Contentful 下载.pdf 文档,如下所示。
问题我遇到的是每次点击“项目注释”链接时,我都会被重定向到错误页面。为什么我的链接没有下载pdf 文件?
在我正在使用的代码下方:
import Client from '../Contentful';
class Sidebar extends React.Component {
state = {
ships: []
};
async componentDidMount() {
let response = await Client.getEntries({
content_type: 'cards'
});
const ships = response.items.map((item) => {
const {
name,
slug,
projectnotes
} = item.fields;
return {
name,
slug,
projectnotes
};
});
this.setState({
ships
});
}
getFilteredShips = () => {
// operations .......
};
render() {
return (
<div className="map-sidebar">
{this.props.activeShipTypes}
<pre>
{this.getFilteredShips().map((ship) => {
console.log(ship);
return (
<Card className="mb-2">
<CardImg />
<CardBody>
<CardTitle>
<h3 className="thick">{ship.name}</h3>
</CardTitle>
<Row style={{ marginTop: '20px' }}>
<div className="buttoncontainer">
<div className="btn btn-cards">
<a className="buttonLink" href={ship.projectnotes}>
Project Notes
</a>
</div>
<div className="btn btn-cards">
<a className="buttonLink" href={ship.distancesandcontours}>
Dist and Contours
</a>
</div>
</div>
</Row>
</CardBody>
</Card>
);
})}
</pre>
</div>
);
}
}
export default Sidebar;
我可以确认我正在正确阅读该字段的名称:
也许我错误地分配了路径?
编辑 2
我也尝试了以下方法,但没有发生下载:
<a className="buttonLink" download={ship.projectnotes} href={ship.projectnotes}>Project Notes</a>
到目前为止我做了什么:
1) 我能够实现文档的链接,但是在我开始使用外部容器Contentful 后,pdf 无法下载,这与我希望的相反。也许我分配路径的方式有误?
2) 在研究了更多这个问题后,我发现了this 帖子,这很有用。看来这个帖子的问题可能是下载?
我有点困惑。感谢您指出正确的方向。
【问题讨论】:
标签: javascript html reactjs contentful