使用JQuery

时间:2018-02-08 05:33:57

标签: jquery tomcat

$(document).on("click", "a.fileDownloadLink", function(e) {
  alert("hello file");
  //e.preventDefault();
  //$.fileDownload("D:/apache-tomcat-7.0.76/webapps/sav/WEB-INF/classes/ConfigDataTable.csv");
  $.fileDownload("https://jqueryfiledownload.apphb.com/FileDownload/DownloadReport/2");
});

以上代码能从URL下载文件吗?我如何在本地系统中下载文件?

$.fileDownload("D:/apache-tomcat-7.0.76/webapps/sav/WEB-INF/classes/ConfigDataTable.csv"); 

如果我尝试这个它不起作用..

如何将本地文件的路径转换为url,以便我可以在用户单击链接时下载相同的文件?我使用tomcat服务器,HTML,Jquery

运行它

1 个答案:

答案 0 :(得分:0)

对你有帮助



$(function(){
   $(".fileDownloadLin").click(function(){
     $.fileDownload("https://jqueryfiledownload.apphb.com/FileDownload/DownloadReport/2");
   });         
}); 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.fileDownload/1.4.2/jquery.fileDownload.js"></script>

<a class="fileDownloadLin">click here</a>
&#13;
&#13;
&#13;

例如:     http://localhost:8080/sav/ConfigDataTable.jsp?fileName=yourfilename

//文件应位于资源文件夹

 try{          
     InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("yourfilename");      response.getOutputStream().flush();
      OutputStream os = response.getOutputStream();
      while ((read = is.read(bytes)) != -1) {
         os.write(bytes, 0, read);
      }
      os.flush();
      os.close();
      is.close();
    }catch(Exception e){
      //...
    }

或直接您可以提供本地路径

try{          
      File f=new File("your path");
      InputStream is = new FileInputStream(f);
      response.getOutputStream().flush();
      OutputStream os = response.getOutputStream();
      while ((read = is.read(bytes)) != -1) {
         os.write(bytes, 0, read);
      }
      os.flush();
      os.close();
      is.close();
    }catch(Exception e){
      //...
    }

如果您使用{strong> $。fileDownload 的http://localhost:8080/sav/ConfigDataTable.jsp?fileName=yourfilename此网址,它将有效。