fix: 没有后缀名无法下载文件的问题

This commit is contained in:
kuaifan 2023-02-22 12:41:36 +08:00
parent ce7037d317
commit 9f7cdc34b4
4 changed files with 18 additions and 6 deletions

View File

@ -18,7 +18,6 @@ use Carbon\Carbon;
use DB;
use Redirect;
use Request;
use Response;
/**
* @apiDefine dialog
@ -1022,7 +1021,7 @@ class DialogController extends AbstractController
}
//
$filePath = public_path($array['path']);
return Response::streamDownload(function() use ($filePath) {
return Base::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $array['name']);
}

View File

@ -1507,7 +1507,7 @@ class ProjectController extends AbstractController
}
//
$filePath = public_path($file->getRawOriginal('path'));
return Response::streamDownload(function() use ($filePath) {
return Base::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $file->name);
}

View File

@ -5,7 +5,6 @@ namespace App\Models;
use App\Module\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Response;
/**
* App\Models\FileContent
@ -97,7 +96,7 @@ class FileContent extends AbstractModel
} else {
$filePath = public_path($content['url']);
}
return Response::streamDownload(function() use ($filePath) {
return Base::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $name);
}
@ -128,7 +127,7 @@ class FileContent extends AbstractModel
if ($download) {
$filePath = public_path($path);
if (isset($filePath)) {
return Response::streamDownload(function() use ($filePath) {
return Base::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $name);
} else {

View File

@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Config;
use Overtrue\Pinyin\Pinyin;
use Redirect;
use Request;
use Response;
use Storage;
use Validator;
@ -3182,4 +3183,17 @@ class Base
throw new ApiException($validator->errors()->first());
}
}
/**
* 流下载,解决没有后缀无法下载的问题
* @param $callback
* @param $name
* @return mixed
*/
public static function streamDownload($callback, $name = null) {
if ($name && !str_contains($name, '.')) {
$name .= ".";
}
return Response::streamDownload($callback, $name);
}
}