简体   繁体   中英

Download files from external url in Laravel/PHP

I want to download files using external urls on click below code i am using and its working fine now error but file is not downloading anywhere.

public function checkCount(Request $request)
    {
        $userid = Session::get('UserId');
        $count = DB::table('download_details')
        ->where('type','=',$request->type)
        ->where('user_id','=',$userid)
        ->get()
        // echo "<pre>"; print_r($count); die;
        ->count();
        if($count >= 2)
        {
            echo 0;
        }
        else
        {
          $data = array(
          'corporate_id' => $request->corporateid,
          'user_id' => Session::get('UserId'),
          'ip_address' => \Request::ip(),
          'mac_address' => '',
          'type' => $request->type,
          'count' => '',
          'date' => date('Y-m-d'),
          'status' => 1
         );
         $insert = DB::table('download_details')->insert($data);
         $name = basename($request->url);
         $path = $request->url;
         // echo $path; die;
         $tempImage = tempnam(sys_get_temp_dir(), $path);
         copy($request->url, $tempImage);
         
         return response()->download($tempImage, $name);
        

        }
    }

My Ajax Code

function saveData(type,url)
    {
         var corporateid = $('#corporateid').val();
         // alert(type);
         $.ajax({
         type:'POST',
         url:'/check-count',
         data:{'_token':'{{csrf_token()}}',corporateid:corporateid,type:type,url:url},
         success:function(response){
          // alert(response);
          if(response == 0)
          {
            $('#download_error_message').show();
            $('#download_error_message').html("Download Limit Exceeded");
            $('#download_error_message').fadeOut(4000);
          }
          else
          {
            alert("Success");
           
          }
         }
        });
    }

Else part shows that i am downloading file. NO errors is ajax request but file is not downloading at all.

file is not downloading.please help

Hopefully, it will work.

public function checkCount(Request $request)
    {
        $userid = Session::get('UserId');
        $count = DB::table('download_details')
        ->where('type','=',$request->type)
        ->where('user_id','=',$userid)
        ->get()
        // echo "<pre>"; print_r($count); die;
        ->count();
        if($count >= 2)
        {
            echo 0;
        }
        else
        {
          $data = array(
          'corporate_id' => $request->corporateid,
          'user_id' => Session::get('UserId'),
          'ip_address' => \Request::ip(),
          'mac_address' => '',
          'type' => $request->type,
          'count' => '',
          'date' => date('Y-m-d'),
          'status' => 1
         );
         $insert = DB::table('download_details')->insert($data);
         $name = basename($request->url);
         $path = $request->url;
         // echo $path; die;
         $tempImage = tempnam(sys_get_temp_dir(), $path);
         copy($request->url, $tempImage);
         
         $mimeType = mime_content_type($tempImage);
         $headers = ["Content-Type: {$mimeType}"];
    
         return Response::download($tempImage, $name, $headers);
        

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM