Last active
January 26, 2024 19:52
-
-
Save thekid/5a0f1d26e3be520397a2d8e53a901ebf to your computer and use it in GitHub Desktop.
HTMX & Vanilla JS & XP Upload, including drag & drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "thekid/upload", | |
"description": "Upload example", | |
"type": "project", | |
"require": { | |
"xp-forge/handlebars-templates": "^3.3", | |
"xp-forge/frontend": "^6.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use util\cmd\Console; | |
use web\Application; | |
use web\frontend\{Frontend, Handlebars, Get, Post, View, Request}; | |
class Upload extends Application { | |
public function routes() { | |
$impl= new class() { | |
#[Get] | |
public function list() { | |
return View::named('upload'); | |
} | |
#[Post('/upload')] | |
public function upload(#[Request] $req) { | |
$uploaded= []; | |
if ($multipart= $req->multipart()) { | |
foreach ($multipart->files() as $name => $file) { | |
Console::write(' Upload ', $file, ' ['); | |
$transferred= $previous= 0; | |
while ($file->available()) { | |
$transferred+= strlen($file->read()); | |
if (($transferred - $previous) > 262144) { | |
$display= sprintf('%.1f', $transferred / 1048576); | |
Console::write("{$display} MB]\033[".(strlen($display) + 4)."D"); | |
$previous= $transferred; | |
} | |
} | |
$uploaded[$name]= sprintf('%.1f', $transferred / 1048576); | |
Console::writeLine("{$uploaded[$name]} MB]\033[K"); | |
} | |
} | |
return View::named('upload')->fragment('success')->with(['uploaded' => $uploaded]); | |
} | |
}; | |
return new Frontend($impl, new Handlebars('.')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://stackoverflow.com/questions/16215771/how-to-open-select-file-dialog-via-js