laravel 5从公共文件夹下载文件

时间:2016-08-17 13:37:16

标签: php ajax

我想在laravel 5中使用ajax从公共文件夹下载文件 我的ajax是

$.ajax({
    url:'{{ url() }}/FileManager/downloadfile',
    type:'post',
    data:{file_name:tableData[0],dir_path:selected_row.attr('dir'),is_cloud:tableData[2]},
    success:function(response) {
        console.log("success");
    },
    error:function(e) {
        console.log("error");
    }
});

我的控制器方法是

public function postDownloadfile(Request $request)
{
    $file_path = public_path().'/'.$request->dir_path.$request->file_name;
    $download_link = link_to_asset($file_path);

    return $download_link;

    $file = FileDB::where("title",$request->file_name)->first();
    $fileType = mime_content_type($file_path);
    $headers = array('Content-Type:'. $fileType,);

    return response()->download($file_path);
}

但结果是成功但没有下载文件

1 个答案:

答案 0 :(得分:0)

您的postDownloadFile会返回应该打开的网址。所以在你的ajax电话中:

success:function(response) {
    // response is the url that should be downloaded
    window.location = response;
},

还会忽略控制器代码的最后四行,因为

return $download_link;
在这些行之前调用

相关问题