HTTP APIs and AJAX Uploads
Build secure upload, preview, import, status, and export endpoints without tying the library to one framework.
Updated
handleAjaxUpload()api()apiHttp()ajaxUploader()What you will learn
- Store uploads safely outside the public directory.
- Expose framework-neutral spreadsheet actions.
- Apply authentication, CORS, CSRF/HMAC, and rate limits.
Handle an uploaded spreadsheet#
$response = MnbExcel::handleAjaxUpload(
$_FILES['spreadsheet'],
[
'directory' => __DIR__ . '/../storage/uploads',
'allowed_extensions' => ['xlsx', 'xls', 'ods', 'csv', 'tsv'],
'max_size_mb' => 250,
'validate_package' => true,
]
);
header('Content-Type: application/json');
echo json_encode($response);Dispatch a structured application action#
$response = MnbExcel::api('preview', [
'path' => $storedPath,
'sheet' => 'Products',
'limit' => 25,
], $pdo);Available workflows include upload, preview, import, import-many, status, and export.
Use the complete HTTP endpoint helper#
$response = MnbExcel::apiHttp([
'bearer_tokens' => [getenv('SPREADSHEET_API_TOKEN')],
'allowed_origins' => ['https://admin.example.com'],
'csrf_token' => getenv('SPREADSHEET_CSRF_TOKEN'),
'hmac_secret' => getenv('API_HMAC_SECRET'),
'rate_limiter' => $rateLimiter,
'rate_limit' => 60,
'rate_window_seconds' => 60,
'base_path' => '/spreadsheet',
], $pdo);
$response->emit();Generate a browser upload client#
echo MnbExcel::ajaxUploader('/spreadsheet-api.php?action=upload', [
'field_name' => 'spreadsheet',
'accept' => '.xlsx,.xls,.ods,.csv,.tsv',
'max_size_mb' => 250,
]);Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue