sparemusic / resumable-js
v1.0.2
2018-08-13 03:05 UTC
Requires
- laravel/framework: ^5.0.0
This package is not auto-updated.
Last update: 2024-09-14 04:54:38 UTC
README
安装
此包可用于Laravel 5.4或更高版本。
您可以通过composer安装此包
composer require sparemusic/resumable-js
在Laravel 5.5中,服务提供程序将自动注册。在Laravel旧版本中,请将服务提供程序添加到config/app.php
文件中
'providers' => [ // ... SpareMusic\ResumableJS\Providers\ResumableServiceProvider::class, ];
使用方法
可恢复请求
要创建一个可恢复请求类,请使用Artisan CLI命令make:resumable-request
php artisan make:resumable-request SomeUpload
生成的类将被放置在app/Http/ResumableRequests
目录中。如果此目录不存在,则在运行make:resumable-request
命令时创建。在此类内部,将有一个设置方法,用于设置上传。
/** * Setup resumable instance */ public function setup() { $chunkPath = Storage::disk('local')->path('chunks'); $uploadPath = Storage::disk('local')->path('uploads'); $this->setChunkPath($chunkPath) ->setUploadPath($uploadPath) ->setValidator(function (UploadedFile $file, ResumableParameters $parameters) { return true; }); }
$chunkPath
变量是存储所有上传文件块的目录(如果不存在,将创建此目录)。$uploadPath
变量是存储完成上传的目录(如果不存在,将创建此目录)。
验证器验证上传的文件。如果返回false,则取消上传。否则,继续运行。
路由
要创建可恢复上传所需的路由,您可以使用Route::resumable()
方法。
Route::resumable("/upload", "UploadController@upload");
您也可以手动完成此操作。
Route::get("/upload", "UploadController@upload"); Route::post("/upload", "UploadController@upload");
控制器
您只需在控制器方法中对可恢复请求进行类型提示。这将设置可恢复上传
public function upload(SomeUpload $upload) { $upload->process(); if ($upload->isComplete()) { // File uploaded, do something with file // $upload->getFilename(true); filename with extension // $upload->getFilepath(); full filepath } return $upload->respond(); }