第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊按鈕時無法在 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條經(jīng)驗 獲得超11個贊

只是為了偽代碼相信您的數(shù)據(jù)會根據(jù)需要返回,我認為您需要在成功回調(diào)中觸發(fā)下載,并使用以下內(nèi)容的變體(可能需要根據(jù)您的需要進行調(diào)整):


$(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條經(jīng)驗 獲得超2個贊

您可以傳遞標頭來強制文件類型和下載


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

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

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

在這里您需要根據(jù)您的文件類型添加標頭


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


查看完整回答
反對 回復 2023-07-20
?
翻翻過去那場雪

TA貢獻2065條經(jīng)驗 獲得超14個贊

 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 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號