繁体   English   中英

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

[英]laravel 5 download file from public folder

我想在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);
}

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

您的postDownloadFile返回应打开的URL。 因此,在您的ajax调用中:

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

控制器代码的最后四行也将被忽略,因为

return $download_link;

在这些行之前被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM