download files from server in react
Baidu found a big cow to write and use it on the project. With Google and IE test, you can download files normally. There will be problems with 360browser, and page switching will miraculously enter the callback of click event downloadDetailData, that is, it will trigger the download of files again. Other browsers will not, but this problem will appear in 360browsers to solve the problem. Or if you have used other methods in the project, can you share it
// https://blog.csdn.net/jiangkai528/article/details/78852777
downloadDetailData=()=>{
var divElement= document.getElementById("downloadDiv");
var downloadUrl=`${apiBasePath}/api/xxxxx/downloadDetailData`;
var params=JSON.stringify({
key:"value"
})
ReactDOM.render(
<form action={downloadUrl} method="post">
<input name="params" type="text" value={params}/>
</form>,
divElement
)
ReactDOM.findDOMNode(divElement).querySelector("form").submit();
ReactDOM.unmountComponentAtNode(divElement);
}
render() {
<div>
<div id="downloadDiv" style={{display:"none"}}> </div>
<Button type="primary" onClick={this.downloadDetailData}></Button>
</div>
}