对带有文件的ajax的响应不起作用 [英] response to ajax with file doesn't work

查看:53
本文介绍了对带有文件的ajax的响应不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jquery jtable发送ajax从服务器获取excel文件,但响应:: download不起作用

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');

推荐答案

JavaScript无法访问文件系统,无法使用Ajax下载文件.尝试使用指向文件的iframe进行下载.

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";

如果您使用jQuery,则可以尝试:

If you use jQuery, you can try:

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

另一种解决方案是使用window.open

Another solution is using window.open

window.open("pathtoyourfle");

这篇关于对带有文件的ajax的响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆