简体   繁体   English

node.js - 打开 pdf 文件

[英]node.js - open pdf file

I have a controller that I'm working with APIs and as a result of these processes, I easily save the pdf from the API as 'filename.pdf' in my folder named 'pdfs'.我有一个正在使用 API 的 controller,由于这些过程,我很容易将 pdf 从 API 保存为 my文件夹中的“spdfnamed.pdf” But after these operations, I want to open this pdf file that I saved in the browser.但是经过这些操作后,我想打开我保存在浏览器中的这个 pdf 文件。 ow can I do that?我该怎么做?

I tried to open it by typing window.open(filename) but I get the error:我试图通过键入window.open(filename)打开它,但我收到错误:

window is not a function window 不是 function

Where am I missing?我在哪里失踪?

    const { 
        deliveryType,
        referenceNo,
    } = req.body.datas
    const config = {
        headers: {
            "Content-Type" : "application/json",
        }
    }
    const { data } = await axios.post(`xxxxx`,{
        deliveryType,
        referenceNo,
    },config)

    const result = (data.result.BarcodeZpl)
    var time = Date.now()
    var filename = `frontend/public/pdfs/label${time}.pdf`;
    `
     here i save the pdf
    `
    window.open(filename)
    res.status(201).json({message:'success'})

You are mixing front-end and back-end code.您正在混合前端和后端代码。 Your axios call and the save of the PDF is happening on your server in the back-end.您的 axios 调用和 PDF 的保存正在后端的服务器上进行。 window.open() is something that happens in the browser (the front-end). window.open()是在浏览器(前端)中发生的事情。 You can't do that in the back-end.你不能在后端这样做。

For the front-end to display a PDF that you have stored in the back-end, you will have to make a request from the front-end to your back-end (some sort of GET request) and then have your back-end stream the PDF that it has stored in its file system to the front-end with the proper content-type so the browser can then do something with it.为了让前端显示您存储在后端的 PDF,您必须从前端向后端发出请求(某种 GET 请求),然后让您的后端stream PDF 已存储在其文件系统中的前端具有正确的内容类型,因此浏览器可以对其进行处理。

You don't show enough overall context for us to see if you should be doing that in the request handler you show some code for or if that should be in a different request handler or if you should be sending back a redirect to the other URL.您没有为我们显示足够的整体上下文来查看您是否应该在显示一些代码的请求处理程序中执行此操作,或者是否应该在不同的请求处理程序中执行此操作,或者您是否应该将重定向发送回另一个 URL . We'd have to understand the entire operation here and what you're trying to do.我们必须了解这里的整个操作以及您要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM