javascript - jquery下载文件的方法

标签 javascript jquery ajax

我通过点击链接发送一个ajax请求,在得到响应后我现在得到了PDF文件的URL,我想在用户的系统上下载该特定文件。但是我无法在用户系统。它正在浏览器网址上打开,我不想向用户显示 PDF 文件的网址。我们如何在 jquery 中做到这一点。

HTML

<li><a href="javascript:void(0)" data-unid ="1" class ="cert_download">Download</a></li>

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;
            }
        }
    }
});

我不想向用户显示 URL,甚至不想在新选项卡/窗口中下载文件。当他单击链接时,我想将文件下载到他的系统中 我们如何将文件下载给用户。请帮助我

最佳答案

这对你有用:

function downloadURI(uri, name){
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    $("#data_cert").html(link);
    link.click();
    $("#data_cert").html("");
}

关于javascript - jquery下载文件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668217/

相关文章:

javascript - 从值数组中获取多个最大值

javascript - 使用 javascript/jquery 从未计算的 css 样式中获取值

javascript - 选择 li 项时更改无序列表的值

javascript - 父级的 Angular-ui-router 嵌套状态 "no such state"

javascript - 类型 'number[]' 缺少类型 '[number, number, number, number]' 的以下属性 : 0, 1、2、3

javascript - 使用 jquery 动态确定的表单变量

javascript - 我什么时候应该在 knockout 中使用括号

java - 使用 JSON 进行 Ajax 调用时出现 400 错误

UpdatePanel 内的 ASP.NET 标签未更新

javascript - java中文件上传更改文件名