ФреймворкJavaScript
Файл: вложения и скачивание
An attachment card for any uploaded file. Shows a type icon, filename, human-readable size, a download action, and an optional caption. Files are sent through a consumer-supplied uploader; when none is provided the tool falls back to a local blob URL (uploadByFile) or the pasted URL itself (uploadByUrl). An optional MIME allowlist and max size can gate what is accepted.
Импорт
TypeScript
import { File } from '@bloklabs/core/tools';Конфигурация
| Опция | Тип | По умолчанию | Описание |
|---|---|---|---|
uploader | FileUploader | undefined | Consumer-supplied uploader with optional `uploadByFile(file, ctx)` and `uploadByUrl(url, ctx)` methods, each resolving to `{ url, fileName?, size?, mimeType? }`. The `ctx.onProgress(percent)` callback reports upload progress. When omitted, files fall back to a blob URL or the pasted URL. |
endpoints | string | { byFile?: string; byUrl?: string } | undefined | Upload endpoint(s) — Blok POSTs the upload itself (multipart/form-data for files, JSON `{ url }` for embedded URLs) and expects a `{ url, fileName?, size?, mimeType? }` body back. A string is used for both; an object targets each separately. An explicit `uploader` always takes precedence. |
field | string | "file" | Form-data field name carrying the uploaded file. |
additionalRequestHeaders | Record<string, string> | undefined | Extra headers merged into endpoint upload requests. |
types | string[] | undefined | Optional MIME allowlist. When omitted, files of any type are accepted. |
sources | 'upload' | 'url' | 'both' | 'both' | Restrict how a file may be added — file upload only, URL only, or both. |
maxSize | MaxSizeConfig | 30 MiB | Max upload size. A number caps every type (bytes); an object caps per MIME type with `'*'` as the fallback. Pass Infinity for unlimited. |
captionPlaceholder | string | undefined | Placeholder text shown in the caption field. |
Формат данных
TypeScript
interface FileData {
url: string; // File source URL — http(s) or blob:
fileName?: string; // Original filename, when known
size?: number; // File size in bytes; rendered human-readable
mimeType?: string; // MIME type; used to pick the type icon
caption?: string; // Plain-text caption
captionVisible?: boolean; // Caption visible in the rendered state (default true)
}JSON
{
"id": "fil001",
"type": "file",
"data": {
"url": "https://example.com/files/report.pdf",
"fileName": "report.pdf",
"size": 184320,
"mimeType": "application/pdf"
}
}Пример использования
TypeScript
import { Blok } from '@bloklabs/core';
import { File } from '@bloklabs/core/tools';
const editor = new Blok({
holder: 'editor',
tools: {
file: {
class: File,
config: {
uploader: {
async uploadByFile(file) {
const url = await myUpload(file);
return { url, fileName: file.name, size: file.size, mimeType: file.type };
},
},
},
},
},
});