tf_swufe 2013-11-14 02:58 采纳率: 0%
浏览 2072

使用jquery请求下载功能的疑问

小弟因为工作需要,需要实现一个下载功能,目前功能已经实现,但是还是存在一些疑问,先把代码贴出来吧,如下,

    <script type="text/javascript">
    $(document).ready(function(){
        $("#exBtn").click(function(){
            $.get("<%=basePath%>/DownloadServlet?methodName=downloadTemplate");
        });
    });
</script>
</head>
<body>
    <a href="<%=basePath%>/DownloadServlet?methodName=downloadTemplate">Download</a>
    <input type="button" id="exBtn" value="下载模板" />
</body>

现在出现的问题就是,我点击下载button或者下载的url(a标签),都能调用到后台的的下载代码,但是通过点击button后台代码虽然执行了,但是前台是没有反应的,而点击url(a标签)则能够正常下载,请问这是为什么呢,求大神解答,感激不尽。。。

下面是后台的部分代码:

FileInputStream in = null;
        ServletOutputStream out = null;
        if (file.exists()) {
            try{
                out = response.getOutputStream();
                in = new FileInputStream(file);
                response.setHeader("Content-Disposition", "attachment;filename=template.xls");
                response.setContentType("application/x-msdownload");
                response.setContentLength((int) file.length());
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = in.read(bytes)) != -1) {
                    System.out.println("Beginning write stream, length:" + len);
                    out.write(bytes, 0, len);
                }
  • 写回答

1条回答 默认 最新

  • franzhong 2013-11-14 23:30
    关注

    url后面加个时间戳试一下&123456...

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用