Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在reactjs(上下文api)中使用axios下载pdf文件时出现的问题_Javascript_Node.js_Reactjs - Fatal编程技术网

Javascript 在reactjs(上下文api)中使用axios下载pdf文件时出现的问题

Javascript 在reactjs(上下文api)中使用axios下载pdf文件时出现的问题,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,当reactjs(使用上下文api)用作前端(使用axios)时,任何人都可以帮助我下载pdf文件吗?在服务器端,如果我单击下载链接,一切都很好-控制台中显示以下响应 这是客户端代码生成请求: const pdfFileDownload = async (dlIdInfo) => { const { id } = dlIdInfo; console.log(`pdf file id`, id); try { const res = await axi

当reactjs(使用上下文api)用作前端(使用axios)时,任何人都可以帮助我下载pdf文件吗?在服务器端,如果我单击下载链接,一切都很好-控制台中显示以下响应

这是客户端代码生成请求:

const pdfFileDownload = async (dlIdInfo) => {
    const { id } = dlIdInfo;
    console.log(`pdf file id`, id);
    try {
        const res = await axios({
            method: "GET",
            url: `/pub/pdfdload/${id}`,
            responseType: 'blob',
            
        });
        console.log(res.data);

    } catch (err) {
        console.log(err.response);
    }

}
这是服务器端代码:

router.get("/pdfdload/:id", async (req, res, next) => {

const fileId = req.params.id;

try {
    const fetchedFileToDl = await FILESDB.findById(fileId);
  
    const fileArr = fetchedFileToDl.pdfFiles;
   
    fileArr.map(filename => {
        const editedFileName = filename.split("/")[1];
        const filePath = path.join(__dirname, "../", "public", editedFileName);
        
        const files = fs.createReadStream(filePath);
        res.setHeader("Content-Type", "application/pdf");
        res.setHeader('Content-Disposition', 'inline; filename="' + editedFileName + '"');
        files.pipe(res);

    });
    

} catch (err) {
    console.log(err);
}});

我通常是这样做的:

const downloadFileAsPDF = (name, data) => {
    const url = window.URL.createObjectURL(new Blob([res.data], {type: `application/pdf`}));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', name);
    link.click();
    window.URL.revokeObjectURL(url);
}
 
const pdfFileDownload = async (dlIdInfo) => {
    const { id } = dlIdInfo;
    console.log(`pdf file id`, id);
    try {
        const res = await axios({
            method: "GET",
            url: `/pub/pdfdload/${id}`,
            responseType: 'blob',
        });

        downloadFileAsPDF('file.pdf', res.data);
    } catch (err) {
        console.log(err.response);
    }

}

这非常简单,但它完成了任务。

它工作正常,但fileArr有多个pdf文件要下载,但只有一个pdf文件要下载,当我在网络标题中检查响应时,内容配置仅显示一个文件名,但在服务器控制台上显示多个文件名。。。原因可能是什么???我们怎样才能下载多个pdf文件??