Javascript Laravel使用AJAX下载xls文件
我无法通过Ajax下载我的XLS文件Javascript Laravel使用AJAX下载xls文件,javascript,ajax,laravel,Javascript,Ajax,Laravel,我无法通过Ajax下载我的XLS文件 function downloadFile(response) { var blob = new Blob([response], {type: 'application/vnd.ms-excel'}) var url = URL.createObjectURL(blob); location.assign(url); } $('#export').click(function () {
function downloadFile(response) {
var blob = new Blob([response], {type: 'application/vnd.ms-excel'})
var url = URL.createObjectURL(blob);
location.assign(url);
}
$('#export').click(function () {
$.ajax({
type: 'POST',
url : 'factures/engagements/selected/export',
headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
dataType: "json",
data : {
checkboxes : checkboxValues
}
}).done(downloadFile);
});
我的控制器:
public function exportFacturesEngagementSelected(Request $request){
$checkboxes = $request->input('checkboxes');
$collection = collect($checkboxes);
$engagements = EngagementOrder::whereIn('id' , $collection)->get();
$date = Carbon::now('Europe/Paris')->format('d-m-Y H:i:s');
$output = Excel::create('factures_engagements' . $date. '', function($excel) use ($engagements) {
$excel->sheet('Excel', function($sheet) use ($engagements) {
$sheet->loadView('engagements.exportExcelSelected')->with("engagements", $engagements);
});
})->export('xls');
return $output;
}
我只在控制台网络中获取文件的输出预览,但在浏览器中什么也没有发生;没有下载任何文件。我做错了什么
更新@matticustard解决方案:
我的checkboxValues是一个json示例,结果是
{920:“920”,927:“927”,931:“931”,939:“939”}
当我执行控制台日志查询时,我得到:
未定义复选框=920&复选框=927&复选框=931&复选框=939&
当我尝试获取控制器的值时,我发出$request->all()命令,得到:
array:2 [
"undefinedcheckboxes" => "920"
"checkboxes" => "939"
]
为什么我得到了未定义的复选框,为什么我没有得到其他ID 试试这个
如果你的ajax成功了
location.href = path/to/file/property_title.xls
换行
->export($type);
与
这几天我用了
(我的建议)将模型集合转换为XLS并导出。
据我所知,您希望通过AJAX下载该文件。我一点也不了解你,但我可以分享我帮助你的经验。这将与正常的POST请求一起工作,但无论如何它都不会刷新您的页面。
这是一个有效的例子:
实际上没有理由使用AJAX或存储文件。您可以通过GET请求将复选框数据作为URL中的参数传递,并使用
下载文件。我猜到了控制器的名称,所以根据需要进行调整
web.php将路由更改为
GET
Route::get('factures/engagement/selected/export','FactureController@exportFacturesEngagementSelected')->name('export');
JavaScript函数和处理程序按照控制器的预期序列化数据并发出请求 我猜,
checkboxValues
是一个基于控制器中采取的操作的值数组。但是您可以直接序列化表单var query=$('form')编码>并调整控制器以匹配输入
函数下载文件(){
var查询;
$.each(复选框值、函数(索引、值){
查询+='复选框='+值+'&';
});
$('')
.attr('src','/factures/engages/selected/export?'+查询)
.hide()
.附于(“主体”);
}
$(“#导出”)。单击(函数(){
下载文件();
});
FactureController.php
并在控制器中使用下载方法
/。。。
->下载('xls');
我不想存储该文件。这是不可能的?我认为不是,你应该在下载@MathieumourAreaUprety之后删除该文件。当然createObjectURL
需要base64编码的数据才能正确工作。谢谢你的回复,我该怎么做?“我该怎么做?”返回base64\u编码($output)在Laravel中,您可能需要使用return\base64\u encode($output)代码>?要使用第二种方法,我认为它应该或多或少类似于-->return“data:application/octetstream;base64”。base64_编码($output)
数据类型:“json”,
需要json响应。感谢您的回复,此解决方案将是我更新我的帖子的最佳解决方案,以便您了解我在解决方案中的实际问题。我相信我已经接近结果了@马蒂卡斯塔德
->store($type, 'public/reports/', true);
public function export(Request $request)
{
$data = $request->get('data'); // get incoming data field
return \Excel::download(new ExcelService($data), "custom-file-name-with-extension.xls");
}
namespace App\Services;
use App\Models\Item;
use Maatwebsite\Excel\Concerns\FromCollection;
class ExcelService implements FromCollection
{
protected $data = null;
public function __construct($data)
{
$this->data = $data;
}
public function collection()
{
$items = Item::where('data', $data)->get(); // some query using incoming $data
return $items;
}
}
Route::post('export', 'ItemController@export')->name('export');