|
| 1 | +# File Uploads |
| 2 | + |
| 3 | +[](https://mvnrepository.com/artifact/io.avaje/avaje-jex-file-upload) |
| 4 | +[](https://javadoc.io/doc/io.avaje/avaje-jex-file-upload) |
| 5 | + |
| 6 | +Module for handling file uploads via multipart form data. |
| 7 | + |
| 8 | +It provides a `FileUploadPlugin` to configure file upload behavior and a `FileUploadService` to access uploaded files in your route handlers. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Add the file upload dependency to your project: |
| 13 | +```xml |
| 14 | +<dependency> |
| 15 | + <groupId>io.avaje</groupId> |
| 16 | + <artifactId>avaje-jex-file-upload</artifactId> |
| 17 | + <version>${avaje.jex.version}</version> |
| 18 | +</dependency> |
| 19 | +``` |
| 20 | + |
| 21 | +## Basic Usage |
| 22 | + |
| 23 | +### Default Configuration |
| 24 | +```java |
| 25 | +// With default configuration |
| 26 | +Jex app = Jex.create().plugin(FileUploadPlugin.create()); |
| 27 | +``` |
| 28 | + |
| 29 | +### Custom Configuration |
| 30 | +```java |
| 31 | +// With custom configuration |
| 32 | +FileUploadPlugin uploadPlugin = |
| 33 | + FileUploadPlugin.create( |
| 34 | + config -> |
| 35 | + config |
| 36 | + .cacheDirectory("/tmp/uploads") |
| 37 | + .maxFileSize(10, MB) |
| 38 | + .maxRequestSize(50, MB) |
| 39 | + .maxInMemoryFileSize(1, MB)); |
| 40 | + |
| 41 | +Jex app = Jex.create().plugin(uploadPlugin); |
| 42 | +``` |
| 43 | + |
| 44 | +## Accessing Uploaded Files |
| 45 | + |
| 46 | +Access uploaded files in your routes using the `FileUploadService`: |
| 47 | +```java |
| 48 | +app.post( |
| 49 | + "/upload", |
| 50 | + ctx -> { |
| 51 | + var uploadService = ctx.attribute(FileUploadService.class); |
| 52 | + |
| 53 | + // Get a single file |
| 54 | + MultiPart file = uploadService.uploadedFile("avatar"); |
| 55 | + |
| 56 | + // Get multiple files with the same field name |
| 57 | + List<MultiPart> files = uploadService.uploadedFiles("documents"); |
| 58 | + |
| 59 | + // Get all uploaded files |
| 60 | + List<MultiPart> allFiles = uploadService.uploadedFiles(); |
| 61 | + |
| 62 | + // Get files grouped by field name |
| 63 | + Map<String, List<MultiPart>> fileMap = uploadService.uploadedFileMap(); |
| 64 | + }); |
| 65 | +``` |
| 66 | + |
| 67 | +## Configuration Options |
| 68 | + |
| 69 | +| Method | Description | |
| 70 | +|--------|-------------| |
| 71 | +| `cacheDirectory("/path/to/dir")` | Sets the directory where uploaded files exceeding the in-memory size limit will be cached. Defaults to the system's temporary directory. | |
| 72 | +| `maxFileSize(size, unit)` | Sets the maximum allowed size for a single uploaded file. Use -1 for no limit. | |
| 73 | +| `maxRequestSize(size, unit)` | Sets the maximum total size for a multipart request, including all files and form data. Use -1 for no limit. | |
| 74 | +| `maxInMemoryFileSize(size, unit)` | Sets the maximum size a file can be before it is written to disk. Files smaller than this are kept in memory. Use 0 to write all files to disk. | |
| 75 | + |
| 76 | +### File Size Units |
| 77 | + |
| 78 | +Available units: `BYTES`, `KB`, `MB`, `GB` |
0 commit comments