How to download PDF automatically using js?

前端 未结 5 1274
不知归路
不知归路 2020-12-05 15:42

My scenario is that PDF file download automatically, then user fills it and when click on submit button in PDF it connect to java servlet and save it in DB.

         


        
5条回答
  •  既然无缘
    2020-12-05 16:20

    Please try this

    (function ($) {
        $(document).ready(function(){
           function validateEmail(email) {
                const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                return re.test(email);
               }
           
           if($('.submitclass').length){
                $('.submitclass').click(function(){
                    $email_id = $('.custom-email-field').val();
                    if (validateEmail($email_id)) {
                      var url= $(this).attr('pdf_url');
                      var link = document.createElement('a');
                      link.href = url;
                      link.download = url.split("/").pop();
                      link.dispatchEvent(new MouseEvent('click'));
                    }
                });
           }
        });
    }(jQuery));
    
    

    Or use download attribute to tag in HTML5

提交回复
热议问题