用户在laravel中下载外部文件时,更改或强制使用该文件的名称

问题描述

我想知道是否有一种方法可以在laravel应用上强制用户请求某个路由,以从该路由下载外部文件响应,并且文件名是否为 name.type 它将以 customname.type

下载到用户系统。

例如,

这是web.php中的路由

Route::get('/testurl',function(){
   //then some have here by some magic it will force the user to download file on an external server on the web and change the original name of the file when a user downloads it
});

我知道这是一项具有挑战性的工作。 好的,任何专家和你们中的任何一个都可以帮助我吗?如果是通过框架之外的某种技术(以我的情况为laravel)完成的,我将很乐意为我提供一些建议。谢谢大家

解决方法

在这种情况下,您可以使用

选项1:

laravel的streamed downloads中的streamDownload()函数

  Route::get('/testurl',function(){
      return response()->streamDownload(function () {
        echo file_get_contents('google/images/125426ds2s.jpg');
    },'pic1.jpg');
  });

请记住,这会将整个文件从外部源读取到内存中,因此可能不是一个好主意,在这种情况下,请使用第二个选项:

选项2

// Fetch the file from the external path
$contents = file_get_contents('google/images/125426ds2s.jpg');

// Save the contents to disc
Storage::disk('local')->put('125426ds2s.jpg',$contents);

// Get the file path within you local filesystem
$path = Storage::url('125426ds2s.jpg');

// Use laravel's download() helper to fetch the file
return response()->download($path,'pic1.jpg',$headers);

选项3

// redirect the user to the absolute url of the file
return \Redirect::to('google/images/125426ds2s.jpg');

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...