【问题标题】:laravel 5 download file from public folderlaravel 5 从公共文件夹下载文件
【发布时间】:2016-08-17 21:47:30
【问题描述】:

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

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

【问题讨论】:

    标签: php ajax


    【解决方案1】:

    您的 postDownloadFile 返回应该打开的 url。所以在你的ajax调用中:

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

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

    return $download_link;
    

    在这些行之前调用。

    【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2016-03-31
    相关资源
    最近更新 更多