如何在React-Stack Overflow中使用window.open设置文件名 最近30次来自stackoverflow.com 2024-07-12T07:53:49 Z https://stackoverflow.com/feeds/question/68362860 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/68362860 2 如何在React中使用window.open设置文件名 雷亚·莱文森 https://stackoverflow.com/users/10693222 2021-07-13T12:56:31Z 2021-11-17T22:14:28Z <p>当URL为</p><pre><code>blob:http://localhost:300/sample.pdf</code></pre><p>目前,它在新选项卡中打开PDF文件,但URL是</p><pre><code>blob:http://localhost:3001/cfd3b31b(本地主机:3001/cfd3b31b)-b4a8-4c65-aead-694fd27f12a8</code></pre><p>我的目标是将名称设置为URL中的pdf文件(而不是下载文件)为了在URL中添加文件名,应该在代码中更改什么</p>(第页)<p>React.js中的代码</p><pre><code>从&quot;导入公理;公理;;const ShowPDF=()=&gt;{const getPdfFileAsBlob=异步()=&gt;{常量url=`http://localhost:5001/pdf`;//强制接收Blob格式的数据常量配置:任意={响应类型:;斑点;,};尝试{const响应=等待axios.get(url,config);//从PDF流创建Blobconst文件=新Blob([response.data]{类型:&quot;应用程序/pdf;,});//从文件生成URLconst fileURL=URL.createObjectURL(文件);//在新窗口上打开URLwindow.open(文件URL);}捕获(错误){console.log(错误);}};返回&lt;按钮onClick={getPdfFileAsBlob}&gt;显示PDF&lt/按钮&gt;;};导出默认ShowPDF;</code></pre><p>Node.js中的代码-server.jscost fs=require(fs)</p>(第页)<pre><code>const express=require(快递);const cors=需要(cors);const excel=要求(exceljs);const应用=表达式();app.use(cors());app.use(express.json());//GET(获取)//将提供Pdf的Pdf路由app.get(“/pdf&”,(req,res)=&gt;{const文件=fs.createReadStream(./public/samplePDF.pdf&quot;);文件.pipe(res);});const端口=process.env。端口||5001;app.listen(端口,()=&gt;{console.log(`Server Started on Port${Port}`);});</code></pre>