Reactjs 在React和show download弹出窗口中创建临时文件并使用axios获取文件

Reactjs 在React和show download弹出窗口中创建临时文件并使用axios获取文件,reactjs,express,axios,fs,Reactjs,Express,Axios,Fs,在我的应用程序中,我遇到了一种情况,必须创建一个具有自定义扩展名的二进制文件,然后将其发送到客户端。客户端请求axios,然后在服务器上在flyin tmp上创建文件,例如: 在Express中,我有如下方法: router.post('/send-some-file',(req,res)=>{ const {id} = req.body /* * Some code for getting information from db is here */ const fileName = "

在我的应用程序中,我遇到了一种情况,必须创建一个具有自定义扩展名的二进制文件,然后将其发送到客户端。客户端请求axios,然后在服务器上在flyin tmp上创建文件,例如:

在Express中,我有如下方法:

router.post('/send-some-file',(req,res)=>{
const {id} = req.body
/*
* Some code for getting information from db is here
*/
const fileName = "myfile.myextension"
const filePath = path.join(__dirname+`../${fileName}`)
fs.writeFile(filePath,"Data From Database",err=>{
    if(!err){
        //send file to client 'axios' , I use res.download
        res.download(filePath,err=>{})
    }   
 } 
})
这段代码是我的服务器代码,创建文件并发送。这段代码的问题是我不知道文件何时被完全下载以删除文件

在React with redux中,我调度了一个操作,并使用axios调用了此方法

知道我想通过浏览器下载文件,或者让用户以我描述的扩展名格式下载文件

响应如下,标题是: 但我不知道如何弹出窗口下载或如何浏览器自动下载文件

你能试试这个吗

 axios.post('some-url/send-some-file','my id',config()).then(res => 
{
    const url = window.URL.createObjectURL(new Blob([res.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'file.pdf');
    document.body.appendChild(link);
    link.click();
}).catch(er=>{})
 axios.post('some-url/send-some-file','my id',config()).then(res => 
{
    const url = window.URL.createObjectURL(new Blob([res.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'file.pdf');
    document.body.appendChild(link);
    link.click();
}).catch(er=>{})