如何发送ajax响应,如下载按钮下载文件Laravel

时间:2018-03-16 09:03:10

标签: ajax laravel

我想在我的视图上显示下载链接按钮(如果我的ajax成功) follwings是我的下载代码,用于发送ajax响应和显示视图

<div class="blog-moder-button">
  <a  href="public_path('files').'/'.$request->file_name" class="button-md dark-button downlds">
    Download PDF
  </a>
</div>

以下是我的控制器

public function 

downloadform(Request $request) {        
  $validator = Validator::make($request->all(),[
    'name' => 'required',
    'email' => 'required|email',
    'file_name' => 'required',
     ]
  );

  if ($validator->passes()) {
    $msg= array();
    // $msg['success'] = '<div class="alert alert-success"> Successfully Registered</div>';//it is working 
    $msg['success'] = '<div class="blog-moder-button"> <a  href="public_path('files').'/'.$request->file_name"  class="button-md dark-button downlds"> Download PDF</a></div>';//it is not working
  }

  return response()->json($msg);
}

1 个答案:

答案 0 :(得分:1)

不要在控制器中渲染html,这是您视图的工作。

要触发文件下载响应,请使用以下命令:

https://myapp.example.com:9200

https://laravel.com/docs/5.6/responses#file-downloads