2

当URL为

斑点:http://localhost:300/sample.pdf

目前,它在新选项卡中打开PDF文件,但URL是

斑点:http://localhost:3001/cfd3b31b-b4a8-4c65-aead-694fd27f12a8

我的目标是将名称设置为URL中的pdf文件(而不是下载文件)为了在URL中添加文件名,应该在代码中更改什么?

React.js中的代码

从“axios”中导入axios;const显示PDF=()=>{const getPdfFileAsBlob=异步()=>{常量url=`http://localhost:5001/pdf`;//强制接收Blob格式的数据常量配置:any={responseType:“blob”,};尝试{const响应=等待axios.get(url,config);//从PDF流创建Blobconst文件=新Blob([response.data]{类型:“application/pdf”,});//从文件生成URLconst fileURL=URL.createObjectURL(文件);//在新窗口上打开URLwindow.open(文件URL);}捕获(错误){console.log(错误);}};return显示PDF;};导出默认ShowPDF;

Node.js-server.jsconst中的代码fs=require(“fs”);

const express=要求(“express”);const cors=require(“cors”);const excel=要求(“exceljs”);const应用=表达式();app.use(cors());app.use(express.json());//GET(获取)//将提供Pdf的Pdf路由app.get(“/pdf”,(req,res)=>{const文件=fs.createReadStream(“./public/samplePDF.pdf”);file.pipe(res);});const端口=process.env。端口||5001;app.listen(端口,()=>{console.log(`Server Started on Port${Port}`);});
1

0

重置为默认值

你的答案

单击“发布您的答案”,表示您同意我们的服务条款并确认您已阅读我们的隐私政策.