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

当我尝试使用 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) {


                }

            });

        });


德玛西亚99
浏览 153回答 3
3回答

呼唤远方

只是为了伪代码相信您的数据会根据需要返回,我认为您需要在成功回调中触发下载,并使用以下内容的变体(可能需要根据您的需要进行调整):$(document).on('click', '.download_request_btn', function(){&nbsp; &nbsp; var request_id = $(this).attr('request_id');&nbsp; &nbsp; console.log(request_id);&nbsp; &nbsp; var formData = new FormData();&nbsp; &nbsp; formData.append('request_id',request_id);&nbsp; &nbsp; jQuery.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "post",&nbsp; &nbsp; &nbsp; &nbsp; url: site_url+"/DownloadAjax",&nbsp; &nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; &nbsp; contentType:false,&nbsp; &nbsp; &nbsp; &nbsp; processData:false,&nbsp; &nbsp; &nbsp; &nbsp; success: function (res) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const data = res;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const link = document.createElement('a');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link.setAttribute('href', data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link.setAttribute('download', 'yourfilename.extensionType'); // Need to modify filename ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link.click();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});

MM们

您可以传递标头来强制文件类型和下载$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

翻翻过去那场雪

&nbsp;if(!empty($fileName) && file_exists(($exportDir.'/'.$fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = route('yourdownloadCSV',['nameFile' => $fileName]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return response()->json([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status' => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'data'&nbsp; &nbsp;=> $data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message'=> trans('messages.item_upload_shop_ok'),&nbsp; &nbsp; &nbsp; &nbsp; ]);public function yourdownloadCSV($nameFile) {&nbsp; &nbsp; &nbsp; &nbsp; ini_set('memory_limit', '9072M');&nbsp; &nbsp; &nbsp; &nbsp; ini_set('MAX_EXECUTION_TIME', '-1');&nbsp; &nbsp; &nbsp; &nbsp; set_time_limit(10*60);&nbsp; &nbsp; &nbsp; &nbsp; $fullFolderZipFile&nbsp; = public_path().'/export/'.date('ym');&nbsp; &nbsp; &nbsp; &nbsp; $filePath&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $fullFolderZipFile.'/'.$nameFile;&nbsp; &nbsp; &nbsp; &nbsp; $nameDownload&nbsp; &nbsp; &nbsp; &nbsp;= "test";&nbsp; &nbsp; &nbsp; &nbsp; if(file_exists($filePath)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $byteS&nbsp; = filesize($filePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mb&nbsp; &nbsp; &nbsp;= number_format($byteS / 1048576, 2) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($mb>10){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filePathZip= ZipUtil::generateZipFromFile($filePath,$fullFolderZipFile,$nameFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nameDownload .=".zip";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filePathZip = $filePath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nameDownload .=".".pathinfo($nameFile, PATHINFO_EXTENSION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mimeType = File::mimeType($filePathZip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response()->download($filePathZip,$nameDownload,[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type' => $mimeType,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Content-Encoding' => 'Shift-JIS'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])->deleteFileAfterSend(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return '';&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript