jeudi 25 février 2016

Laravel $.post for download File?

I have view.blade.php like this

....
<meta name="_token" content="{!! csrf_token() !!}" />
....
<script>
jQuery(document).ready(function ($) {
    $.ajaxSetup({
        headers: {'X-CSRF-Token': $('meta[name="_token"]').attr('content')}
    });
    $.post('/download_file', {file_name: "abc"}, function (data) {
        console.log(data);
    });
});
</script>

In route.php I already set route

Route::post('/download_file' , 'DownloadController@load_file');

In DownloadController.php I write code for create and download file like this

<?php
namespace App\Http\Controllers;

use Response;
use File;
use Illuminate\Http\Request;

class DownloadController extends Controller {

    public function load_file(Request $request){
        if($request->file_name === "abc"){
            File::put("files/abc.txt", "This is content in txt file");
            $file_abc = public_path() . "/files/abc.txt";
            return Response::download($file_abc);
        }
    }

}

File abc.txt is create on server but browser doesn't download it after $.post call. In console.log(data) I see content of file. Thank for any help.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/21uokZH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire