response to ajax with file doesn't work

戏子无情 提交于 2019-12-11 10:37:18

问题


I use jquery jtable send ajax to get excel file from server but Response::download doesn't work

  $writer = (new WriterFactory())->createWriter(new  Excel5(public_path().'/file/myExport.xls'));
  $phpExcel = $writer->convert($workbook);
  $writer->write($phpExcel);

 Response::download(public_path().'\file\myExport.xls');

回答1:


Javascript cannot access file system, you cannot use ajax to download files. Try using an iframe pointing to the file to download it.

<iframe id="downloadFrame" style="display:none"/>

When you need to download, use this script:

var iframe = document.getElementById("downloadFrame");
iframe.src = "yourpathtofile";

If you use jQuery, you can try:

$("#downloadFrame").attr("src","yourpathtofile");

Another solution is using window.open

window.open("pathtoyourfle");


来源:https://stackoverflow.com/questions/18709783/response-to-ajax-with-file-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!