how to download a pdf file stored in a server in client side in node.js server

前端 未结 2 773
眼角桃花
眼角桃花 2021-02-10 02:29

How to allow a client to download a pdf file stored in a server using node.js.

Please, someone help me out with this code.

fs.readFile(\'temp/xml/user/us         


        
2条回答
  •  耶瑟儿~
    2021-02-10 03:15

    Send the correct mime-type, and then the content of the pdf.

    fs.readFile('temp/xml/user/username.pdf',function(error,data){
        if(error){
           res.json({'status':'error',msg:err});
        }else{
           res.writeHead(200, {"Content-Type": "application/pdf"});
           res.write(data);
           res.end();       
        }
    });
    

    I'm assuming res is your response object.


    Ah but you are using Express. Use Jonathan's answer instead.

提交回复
热议问题