为了账号安全,请及时绑定邮箱和手机立即绑定

单击按钮时无法在 laravel ajax 中下载文件

单击按钮时无法在 laravel ajax 中下载文件

德玛西亚99 2023-07-20 17:04:57
当我尝试使用 laravel ajax 中的下载按钮下载文件时,它无法正常工作,并且我无法下载文件。下面是我的代码:<button type="button" request_id="'.$data->id.'" class="btn btn-success download_request_btn" > Download </button>';控制器:public function downloadReport(Request $request)    {        $request_id = $request->request_id;        $downloadReport = Upload::where('id', $request_id)->first();        $upload_report = $downloadReport->upload_report;        $headers = array(            'Content-Type: application/pdf',            'Content-Type: application/docx',          );        $url= url('storage/documents/request/'. $upload_report);        return response()->download($url);    }阿贾克斯:$(document).on('click', '.download_request_btn', function(){            var request_id = $(this).attr('request_id');           console.log(request_id);           var formData = new FormData();            formData.append('request_id',request_id);            jQuery.ajax({                type: "post",                url: site_url+"/DownloadAjax",                data: formData,                contentType:false,                processData:false,                success: function (res) {                }            });        });
查看完整描述

3 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

只是为了伪代码相信您的数据会根据需要返回,我认为您需要在成功回调中触发下载,并使用以下内容的变体(可能需要根据您的需要进行调整):


$(document).on('click', '.download_request_btn', function(){

    var request_id = $(this).attr('request_id');

    console.log(request_id);

    var formData = new FormData();

    formData.append('request_id',request_id);

    jQuery.ajax({

        type: "post",

        url: site_url+"/DownloadAjax",

        data: formData,

        contentType:false,

        processData:false,

        success: function (res) {

            const data = res;

            const link = document.createElement('a');

            link.setAttribute('href', data);

            link.setAttribute('download', 'yourfilename.extensionType'); // Need to modify filename ...

            link.click();

        }

    });

});


查看完整回答
反对 回复 2023-07-20
?
MM们

TA贡献1886条经验 获得超2个赞

您可以传递标头来强制文件类型和下载


$file_path = storage_path('documents/request/'. $upload_report);

$headers = array('Content-Type'=> 'application/pdf');

return \Response::download($file_path, 'file.pdf', $headers);

在这里您需要根据您的文件类型添加标头


参考链接https://laravel.com/docs/8.x/responses#file-downloads


查看完整回答
反对 回复 2023-07-20
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

 if(!empty($fileName) && file_exists(($exportDir.'/'.$fileName))) {

            $data = route('yourdownloadCSV',['nameFile' => $fileName]);

        }


        return response()->json([

            'status' => 1,

            'data'   => $data,

            'message'=> trans('messages.item_upload_shop_ok'),

        ]);


public function yourdownloadCSV($nameFile) {

        ini_set('memory_limit', '9072M');

        ini_set('MAX_EXECUTION_TIME', '-1');

        set_time_limit(10*60);


        $fullFolderZipFile  = public_path().'/export/'.date('ym');

        $filePath           = $fullFolderZipFile.'/'.$nameFile;

        $nameDownload       = "test";


        if(file_exists($filePath)) {

            $byteS  = filesize($filePath);

            $mb     = number_format($byteS / 1048576, 2) ;

            if($mb>10){

                $filePathZip= ZipUtil::generateZipFromFile($filePath,$fullFolderZipFile,$nameFile);

                $nameDownload .=".zip";

            }else{

                $filePathZip = $filePath;

                $nameDownload .=".".pathinfo($nameFile, PATHINFO_EXTENSION);

            }


            $mimeType = File::mimeType($filePathZip);

            return response()->download($filePathZip,$nameDownload,[

                'Content-Type' => $mimeType,

                'Content-Encoding' => 'Shift-JIS'

            ])->deleteFileAfterSend(true);

        }

        return '';

    }


查看完整回答
反对 回复 2023-07-20
  • 3 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信