admin管理员组文章数量:1290975
I am getting a file url from an API e.g '.jpg'. This is my current code:
<a href={item.mediaUrl} target="_blank" className="ml-2">
<i className="fa fa-download"></i></a>
But i want to download the file directly without opening the file in new browser tab. I also tried an onClick event on the button:
//HTML
<button onClick={() => this.downloadFile(item.mediaUrl)}>Download</button>
//Function
downloadFile = (url) => {
window.open(url);
}
This is also opening the file in a new browser tab. How do I download a file from url directly in reactjs, without opening the file in new browser tab?
I am getting a file url from an API e.g 'http://example./sample.jpg'. This is my current code:
<a href={item.mediaUrl} target="_blank" className="ml-2">
<i className="fa fa-download"></i></a>
But i want to download the file directly without opening the file in new browser tab. I also tried an onClick event on the button:
//HTML
<button onClick={() => this.downloadFile(item.mediaUrl)}>Download</button>
//Function
downloadFile = (url) => {
window.open(url);
}
This is also opening the file in a new browser tab. How do I download a file from url directly in reactjs, without opening the file in new browser tab?
Share Improve this question edited Apr 26, 2018 at 9:45 Martin Reiche 1,6721 gold badge16 silver badges27 bronze badges asked Apr 26, 2018 at 8:08 Sameer ThiteSameer Thite 2,4693 gold badges11 silver badges13 bronze badges4 Answers
Reset to default 2Did you try adding download attribute to the anchor tag ?
<a href={item.mediaUrl} target="_blank" className="ml-2" download>
<i className="fa fa-download"></i></a>
Try removing target="_blank"
from your initial code.
i.e.
<a href={item.mediaUrl} className="ml-2" download>
<i className="fa fa-download"></i></a>
Instead of triggering the download from the user click, do it through the code:
export function downloadButton(link: string, downloadedFileName: string){
return (
<button onClick={() => downloadFile(link, downloadedFileName)}>Download external link</botton>
);
}
export async function downloadFile(link: RequestInfo, fileName: string): Promise<void> {
try {
const response = await fetch(link);
const blob = await response.blob();
const url = window.URL.createObjectURL(new Blob([blob]));
const anchorElement = document.createElement("a");
anchorElement.href = url;
anchorElement.setAttribute("download", fileName);
anchorElement.target = "_blank";
document.body.appendChild(anchorElement);
anchorElement.click();
anchorElement?.parentNode?.removeChild(anchorElement);
} catch (error) {
console.error(error);
}
}
This solution was verified with external links, meaning links that the domain in the URL is different from the current site domain
Are you using react router ? If so can you try below
import { Link } from 'react-router-dom';
<Link to=`/${item.mediaUrl}` onClick={e => e.preventDefault();}>
<i className="fa fa-download"></i></Link>
Link desc Here
版权声明:本文标题:javascript - Reactjs download file without anchor tag or without opening a file in new tab - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523092a2383305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论