显示从快递服务器发送到反应方的pdf文件

时间:2019-01-15 11:12:57

标签: javascript reactjs

我正在像这样从快递服务器发送pdf文件-

    inp = "Python is interactive"
    import re

    split = re.split(' ', inp)
    length = []
    for item in range(len(split)):
        length.append(len(split[item]))

    Max = (max(length))

    for i in range(len(split)):
        print(split[i])

我想在反应侧显示它(注意-显示pdf,不下载) 我尝试将其转换为Blob,但显示空白pdf

我在反应端单击时调用下面的功能

  app.get("/pdf", (req, res) => {
    var file = fs.createReadStream('./public/file.pdf');
    res.contentType("application/pdf");
    res.send(file);
   })

我想显示pdf而不使用服务器pdf文件的静态网址 喜欢-

 viewHandler = async () => {
        axios.get('http://localhost:4000/pdf')
            .then(response => {
                console.log(response.data);

                //Create a Blob from the PDF Stream
                const file = new Blob(
                    [response.data],
                    { type: 'application/pdf' });
                //Build a URL from the file
                const fileURL = URL.createObjectURL(file);
                //Open the URL on new Window
                console.log("Asdasd");

                window.open(fileURL);
            })
            .catch(error => {
                console.log(error);
            });
    };

0 个答案:

没有答案