[英]How to download a file in jquery
I am sending a ajax request on click of LINK from there i am getting a URL of PDF file now after getting the response i want to download that particular file on user's system.But i am not able to download that particular file on user system.It is getting opened on the browser url, i don't want to show the url of PDF file to the user.How can we do this in jquery. 我从那里单击链接时发送ajax请求,得到响应后,我现在得到PDF文件的URL,我想在用户系统上下载该特定文件。但是我无法在用户系统上下载该特定文件。它正在浏览器的URL上打开,我不想向用户显示PDF文件的URL。我们如何在jquery中做到这一点。
HTML HTML
<li><a href="javascript:void(0)" data-unid ="1" class ="cert_download">Download</a></li>
Jquery jQuery的
$.ajax({
type:"POST",
url: myurl,
dataType: "json",
data:data,
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("There is some problem in our system please try after sometimes");
},
success: function(data){
if(data.url){
if (!window.location.origin){
window.location.origin = window.location.protocol+"//"+window.location.host+"/";
location.href = window.location.origin+data.url;
}
}
}
});
I don't want to show user the URL and even i don't want to download the file in a new tab/window.As he click on link i want to download the file to his sytem How can we download the file to the user.Please help me in this 我不想向用户显示URL,甚至也不想在新标签页/窗口中下载文件。当他单击链接时,我想将文件下载到他的系统中。我们如何将文件下载到用户。请帮助我
This will work for u : 这将适用于您:
function downloadURI(uri, name){
var link = document.createElement("a");
link.download = name;
link.href = uri;
$("#data_cert").html(link);
link.click();
$("#data_cert").html("");
}
Just open new window with 只需打开新窗口
window.open('link to pdf', '_blank')
And send the pdf with 并发送带有
header('Content-Disposition: attachment; filename=some.pdf')
Window will close as soon someone click download. 只要有人单击下载,窗口就会关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.