js 实现下载功能

来源:互联网 发布:mac压缩pdf文件大小 编辑:程序博客网 时间:2024/06/05 11:27

这两天在做下载,一般<a href="http://www.baidu.com/test.rar"> test.rar </a> 即可,但是我这里却需要在下载前进行判断,判断成功了才进行下载,

其实网上有这个说法,就是<a href="http://www.baidu.com/test.rar" onclick="test()"> test.rar </a> 说的是先执行test()如果返回true了,就会进行 href的动作,我模拟了,是对的,但是有个问题,我使用的是dropwizard框架,不知道怎么做 http://www.baidu.com/test.rar,因为我根本就没有http://www.baidu.com/test.rar和这个resource,因此我这个不能使用


// if(id==1){
// showMessage("dddd");
// }else{
// window.open("htpp://www.baidu.com/test.rar");
// }
// return false;  返回 false的时候不能进行href操作,

因此我进行了op+='<a  onclick="hasPerm('+cggl.fileId+')"  title=""><b class="fa fa-download"></b></a>';

function hasPerm(fileId){
    console.log(fileId);
    $.get("/cggl/hasFilePerm/"+fileId,function(data){
        console.log(data);
        if(data.result=="success"){
//            window.open("htpp://www.baidu.com/test.rar");
//            window.open(filePath);    
//            window.open(data.filePath);
            $.get("/cggl/downloadCgglFile/"+fileId);
            var queryUrl = "/cggl/downloadCgglFile/"+fileId;
            $.fileDownload(queryUrl).done(function(){
                  PIMS.Tool.unblockUI();  
              }).fail(function(){
                alert('导出失败,请重试,如果问题依然存在请联系管理人员!');  
                  PIMS.Tool.unblockUI();
              });
        }else{
            showMessage("您没有查看此文件的权限");
        }
    });
}

说一下为什么不使用  window.open(data.filePath);原因是我只存储了文件路径,window.open()需要全部路径,我又不想绑死,因此重新通过fileId取得,在后台进行文件流的返回

有空得看下jquery.fileDownload()

0 0