javascript - 在 React 中使用 Rest 调用将 URL 设置为超链接
问题描述
我必须使用超链接下载一些文件。单击下载链接时,将从服务器接收 URL 的值。
const [downloadEpi,setDownloadEpi] = useState({link:'',name:''});
const premiumDownload=(email,epiId,quality)=>{
const download ={
email: email,
episodeId: epiId,
quality: quality
};
axios.post(`http://127.0.0.1:9094/api/tv-series/premiumDownload`,download,{
'Content-Type':'application/json'
}).then(res=>{
const downloadRes = res.data;
setDownloadEpi({link:downloadRes.downloadLink,name:downloadRes.epiName});
}).catch(error=>{
console.log(error);
});
}
<DownloadButton premium onClick={()=>premiumDownload('benz@gmail.com','viki_s01_e01','480p')}>
<a href={downloadEpi.link && downloadEpi.link} download={downloadEpi.name && downloadEpi.name} className="link">download with IDM</a>
</DownloadButton>
当我单击下载按钮时,将在实际值呈现之前使用空字符串调用链接。所以我必须点击按钮两次才能下载。
解决方案
我已经以一种方式解决了它,它会影响性能但工作正常
禁用高级下载功能并通过道具传递请求值。
在 componentDidMount 中调用端点
{下载Epi.link ? “使用 IDM 下载”:“正在加载...”}useEffect(()=>{ const download ={ email: props.email, episodeId: props.epiId, quality: props.quality }; axios.post(`http://127.0.0.1:9094/api/tv-series/premiumDownload`,download,{ 'Content-Type':'application/json' }).then(res=>{ const downloadRes = res.data; setDownloadEpi({link:downloadRes.downloadLink,name:downloadRes.epiName}); }).catch(error=>{ console.log(error); }); },[]);
推荐阅读
- javascript - vuejs注册组件问题
- vba - 运行时错误 1004 - Excel 宏 - 代码之前运行良好
- amazon-web-services - 将静态网站内容的 zip 文件上传到 S3,但网站显示错误 404
- visual-studio - 新的 ASP.NET Core 项目存在构建错误?
- facebook - 显示网站从 Facebook 登录获得的数据
- python - 是否可以重新训练以前保存的 keras 模型?
- python - 从格式化为列/行的python-numpy矩阵获取列/行向量
- vba - 同时循环遍历一行中的所有单元格和一个命名范围
- java - 使用Spring Boot时如何运行所有测试类但只启动嵌入式Tomcat一次?
- mysql - 在类似查询的 mysql 中包含点运算符