Skip to content

Commit 514c328

Browse files
committed
fix: 大文件上传示例
1 parent 05568fa commit 514c328

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

laravel/app/Http/Controllers/Bus/AutoController.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers\Bus;
44

55
use App\Http\Repository\AccessToken;
6+
use AuroraLZDF\Bigfile\Traits\TraitBigfileChunk;
67
use Curl\Http;
78
use Illuminate\Http\Request;
89
use App\Http\Controllers\Controller;
@@ -11,6 +12,8 @@
1112

1213
class AutoController extends Controller
1314
{
15+
use TraitBigfileChunk;
16+
1417
/**
1518
* @var Http 请求类
1619
*/
@@ -20,6 +23,45 @@ public function __construct()
2023
{
2124
$this->http || $this->http = Http::getInstent();
2225
}
26+
27+
/**
28+
* 展示视图
29+
*
30+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
31+
*/
32+
public function loadView()
33+
{
34+
$data = [
35+
'chunk_size' => config('bigfile.chunk_size'),
36+
'max_size' => config('bigfile.max_size'),
37+
'user_id' => Auth()->id(),
38+
];
39+
return view('bigfile.bigfile', compact('data'));
40+
}
41+
42+
/**
43+
* 上传操作
44+
*
45+
* @param Request $request
46+
* @return \Illuminate\Http\JsonResponse
47+
*
48+
* @throws \Exception
49+
*/
50+
public function upload(Request $request)
51+
{
52+
$data = $request->all();
53+
$action = $request->input('act');
54+
55+
if (isset($action) && $action == 'upload') {
56+
57+
$result = $this->uploadToServer($data);
58+
return response()->json($result);
59+
}
60+
61+
$result = $this->uploadChunk($request);
62+
return response()->json($result);
63+
}
64+
2365
/**
2466
* 获取七牛 Token 的方法
2567
*

laravel/routes/web.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@
7878
return view('test');
7979
});
8080

81-
Route::middleware('web')->get('/upload/bigfile', '\AuroraLZDF\Bigfile\Controllers\BigfileController@loadView')->name('bigfile_view');
81+
Route::middleware('web')->get('/upload/bigfile', 'Bus\AutoController@loadView')->name('bigfile_view');
8282
// bindings:不限制API访问次数限制,不需要 csrf_token 验证
83-
Route::middleware('bindings')->post('/upload/bigfile', '\AuroraLZDF\Bigfile\Controllers\BigfileController@upload')->name('bigfile_upload');
83+
Route::middleware('bindings')->post('/upload/bigfile', 'Bus\AutoController@upload')->name('bigfile_upload');

0 commit comments

Comments
 (0)