@nexim/upload-sdk
Version:
TypeScript SDK for seamless integration with Nexim Media Upload Service. It provides state machine-based upload handling, progress tracking, and type-safe API for image optimization and file uploads.
63 lines (48 loc) • 2.19 kB
Markdown
[@nexim/upload-sdk](../README.md) / pickAndProcessFile
# Function: pickAndProcessFile()
> **pickAndProcessFile**(`accept`: `string`, `callback`: (`file`: `File`) => `MaybePromise`\<`void`\>): `void`
Creates and triggers a virtual file input element to handle file selection.
This utility creates a temporary file input element, attaches event handlers,
and removes itself after use.
Features:
- Creates a hidden file input element
- Handles file selection through browser's native interface
- Automatically cleans up after selection
- Supports async callbacks for processing selected files
- Filters files based on accept parameter (MIME types)
## Parameters
| Parameter | Type | Description |
| ---------- | -------------------------------------------- | --------------------------------------------------------------------------------------------- |
| `accept` | `string` | Comma-separated list of allowed file types (e.g., 'image/\*', '.pdf', 'image/jpeg,image/png') |
| `callback` | (`file`: `File`) => `MaybePromise`\<`void`\> | Function to handle the selected file. Can be async for long-running operations. |
## Returns
`void`
## Examples
```typescript
// Example 1: Pick an image and upload it using uploadImage
import { pickAndProcessFile, uploadImage } from '@nexim/upload-sdk';
pickAndProcessFile('image/*', async (file) => {
await uploadImage(file, {
path: 'images/user-avatar',
auth: { id: '...', token: '...' },
description: 'User Avatar',
apiEndpoint: 'https://api.example.com/upload',
preset: {
client: { width: 512, height: 512, quality: 90 },
// ...
},
});
});
```
```typescript
// Example 2: Pick a PDF and upload it using uploadFile
import { pickAndProcessFile, uploadFile } from '@nexim/upload-sdk';
pickAndProcessFile('.pdf', async (file) => {
await uploadFile(file, {
path: 'documents/report.pdf',
auth: { id: '...', token: '...' },
description: 'Monthly Report',
apiEndpoint: 'https://api.example.com/upload',
});
});
```