The following guide will help you implement fancier file uploads for your web apps.
Laravel: Laravel is a web application framework with expressive, elegant syntax. Supported versions > 5.x.x . The version used in this guide is Laravel 8.
Uppy: Sleek, modular open-source JavaScript file uploader.
Tus: Open Protocol for Resumable File Uploads.
Tus-PHP: A pure PHP server and client for the tus resumable upload protocol v1.0.0.
Redis: An open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.
1.Install Uppy and its supported plugins (Dashboard, Tus) with NPM.
npm install uppy
npm install @uppy/dashboard
npm install @uppy/tus
<button type='button' class='btn btn-success btn-sm' id='file_upload' name='file_upload'>Upload File</button>
<meta name="csrf-token" content={"{ csrf_token() }}">
import Uppy from '@uppy/core';
import Tus from '@uppy/tus'
import Dashboard from '@uppy/dashboard';
const uppy = new Uppy({
debug: false,
autoProceed: false,
restrictions: {
maxFileSize: 1000000,
maxNumberOfFiles: 1,
allowedFileTypes: ['image/*'],
}
})
.use(Dashboard, {
target: 'body',
trigger: '#file_upload',
closeAfterFinish: true,
browserBackButtonClose: false,
showProgressDetails: true,
})
.use(Tus, {
endpoint: '/tus', // use your tus endpoint here
retryDelays: [0, 1000, 3000, 5000],
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
redis-server
redis-cli ping
composer require ankitpokhrel/tus-php
php artisan make:provider TusServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use TusPhp\Tus\Server as TusServer;
class TusServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
$this->app->singleton('tus-server', function ($app) {
$server = new TusServer('redis');
$server->setApiPath('/tus');
$server->setUploadDir(storage_path('app/public/uploads'));
return $server;
});
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
Route::any('/tus/{any?}', function () {
return app('tus-server')->serve();
})->where('any', '.*');
That’s all folks. Users will be able to resume the upload of files after a network error or closing the browser. Hit the comments if you need any help.
Also Published here