Questions & Answers

How can I handle file uploads?

Deepkit HTTP
50 up-votes
Warning: The answer is generated by an artificial intelligence. It might not be correct.
To adjust rating, open the thread in Discord and click on the up/down vote button.

You can handle file uploads using the UploadedFile type and the HttpBody type. The UploadedFile type represents an uploaded file and provides information about the file, such as its size, name, type, and path. The HttpBody type allows you to receive and validate the uploaded file as part of the request body. Here's an example:

import { HttpBody, UploadedFile } from '@deepkit/http';

class FileUpload {
    file!: UploadedFile;
}

router.post('/upload', (body: HttpBody<FileUpload>) => {
    const uploadedFile = body.file;
    // Handle the uploaded file
});

You can access the properties of the UploadedFile object, such as size, name, type, and path, to perform further processing or storage of the uploaded file.

An example HTML form for this route would look like this:

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>