简体   繁体   English

在 Laravel/PHP 中从外部 url 下载文件

[英]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.我想使用外部 url 下载文件,点击我正在使用的下面的代码,现在它工作正常错误,但文件没有在任何地方下载。

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我的 Ajax 代码

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.没有错误是 ajax 请求,但文件根本没有下载。

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);
        

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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