lundi 10 février 2020

Download txt file with laravel and axios

Hello there Hope you will be doing good.I want to download txt file generated on the fly from the controller of laravel i have search alot but could not find any solution.Please help out i will be very thankful.

Blade code with axios request

submitHandler:function(form,e){
            var btn=document.querySelector("#BtnSubmit");
            btn.style.display="none";var img=document.createElement("img");
            img.setAttribute("src",base_url+'front/images/loading.gif');
            var loader=document.querySelector("#loader");loader.appendChild(img);
            var url="<?php echo route('database.export-txtProcess');?>";
            var cur_url="<?php echo route('database.export-txt');?>";

            //var tblExportSelect  = $("#tblExportSelect").val();


            var pushArray = [];
            $.each($("#tblExportSelect option:selected"), function(){
            pushArray.push($(this).data("id"));
            });

            var data  = new FormData();

            data.append('tblExportSelect',pushArray);


            //$("#tblExportSelect").val(selected);


            axios({
                method: 'POST',
                url: url,
                data: data,
              })
            .then(function(res){
              console.log(res);
            })
         e.preventDefault();
      }
   });

Controller Method

public function exportTxtProcess(Request $request){

      /*dd($request->tblExportSelect);*/

       $tables  = explode(",", $request->tblExportSelect);

        $destinationPath = public_path('/');

        $result;

       foreach ($tables as $table) {
          $outputs   =  DB::select("SELECT * FROM $table");

          $today   = date("Y-m-d");
          $fileName  = $table."-".$today;
          $fp = fopen($destinationPath . "$fileName.txt","wb");

          foreach ($outputs  as $output) {
            $output  = (array)$output;

            @array_shift($output);

            $removeUserId  = @$output['user_id'];
            $created_at    = @$output['created_at'];
            $updated_at    = @$output['updated_at'];



            if (($key = array_search($removeUserId, $output)) !== false) {
                unset($output[$key]);
            }
            if (($key1 = array_search($created_at, $output))) {

                unset($output[$key1]);
            }

            if (($key2 = array_search($updated_at, $output))) {

                unset($output[$key2]);
            }

            if (is_null($created_at) OR $created_at == '') {
                unset($output['created_at']);
            }

            if (is_null($updated_at) OR $updated_at == '') {
                unset($output['updated_at']);
            }


            $netResult = $this->getTableFields($table,$output);

            fwrite($fp,$netResult);

          }

          $result = fclose($fp); 
       }
       /*$arr = array(['Good' => true,'message' => 'Data has been successfully imported.'], 200);
                    echo json_encode($arr);*/
       if ($result) {
        $pathToFile  = $destinationPath . "$fileName.txt";

        $downloaded = response()->download($pathToFile)->deleteFileAfterSend();


       }
    }

I want to download when txt file which is created as above but instead of download it streaming in the console.

Thank in advance



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OKj8lY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire