[英]How to download PDF automatically using js?
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.我的场景是自动下载PDF文件,然后用户填写它,当点击PDF中的提交按钮时,它连接到java servlet并将其保存在数据库中。
In my Application just the 2nd point is missing.在我的应用程序中,仅缺少第二点。 Please provide code how to interact with extension using JavaScript to download file automatically.请提供代码如何使用 JavaScript 与扩展交互以自动下载文件。 I just want to download the file.我只想下载文件。
Use the download attribute .使用下载属性。
var link = document.createElement('a');
link.href = url;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
It is also possible to open the pdf link in a new window and let the browser handle the rest:也可以在新窗口中打开 pdf 链接,让浏览器处理其余部分:
window.open(pdfUrl, '_blank');
or:或者:
window.open(pdfUrl);
/* Helper function */
function download_file(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
var filename = fileURL.substring(fileURL.lastIndexOf('/')+1);
save.download = fileName || filename;
if ( navigator.userAgent.toLowerCase().match(/(ipad|iphone|safari)/) && navigator.userAgent.search("Chrome") < 0) {
document.location = save.href;
// window event not working here
}else{
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
}
// for IE < 11
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
How to use?如何使用?
download_file(fileURL, fileName); //call function
Source: convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/来源: convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/
eg you're using php and $filepath contains pdf file path.例如,您正在使用 php 并且 $filepath 包含 pdf 文件路径。
so you can write javascript like to to emulate download dialog box.所以你可以编写 javascript 来模拟下载对话框。
<script language="javascript">
window.location.href = '<?php echo $filepath; ?>';
</script
Above code sends browser to pdf file by its url " http://www.domain.com/files/filename.pdf ".上面的代码通过其 url “ http://www.domain.com/files/filename.pdf ”将浏览器发送到 pdf 文件。 So at last, browser will show download dialog box to where to save this file on your machine.所以最后,浏览器会显示下载对话框到你机器上保存这个文件的位置。
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));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="post"> <div class="form-item form-type-textfield form-item-email-id form-group"> <input placeholder="please enter email address" class="custom-email-field form-control" type="text" id="edit-email-id" name="email_id" value="" size="60" maxlength="128" required /> </div> <button type="submit" class="submitclass btn btn-danger" pdf_url="https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf">Submit</button> </form>
Or use download attribute to tag in HTML5或者使用下载属性在HTML5中进行标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.