您的位置:首页 > Web前端 > JQuery

jquery ajax下载 文件 思路

2016-11-08 18:22 113 查看
1.   因为  ajax   反回的数据是xml、text、json、html  等类型  而没有  io (流)   所以不能直接下载

思路 是:

   跳转  到一个action 1 取到地址     跳转的另一个  action 2  的地址把参数带上   在action2   中查询到这个文件  来写下载

页面

  $(function(){

    $.ajax({

        url:"TestServer",

        type:"get",

        success:function(date){

            window.location.href=date;

        }

    })

});

   action1   简单实例  TestServer

        PrintWriter out = response.getWriter();

        out.write("Test1Server");  //简单  这个就是action2   的路径   反回去

        out.flush();

action2

        File file  = new File("D:/test.txt");

        FileInputStream  in  = new FileInputStream(file);

        response.setContentType("application/octet-stream");

        response.setHeader("Content-disposition",

                   "attachment; filename=" +"test" + ".txt");

        ServletOutputStream out = response.getOutputStream();

        byte[] b  = new byte[1024];

        int s =0;

        while((s=in.read(b))>0){

            out.write(b);

        }

        out.flush();

        in.close();

        out.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: