中文 (简体)
Fork Me on GitHub

Concept

The HttpController provides simple uploading approach that let you upload files via an HTTP POST request.

How To Use?

Uploading a file is just easy as you doing other stuffs in an SFN application, you just need to configure some options, and the rest work will be handled by the framework automatically.

In a HttpController, use the decorator @upload to set accept fields that may contain files.

Example

import { HttpController, Request, route, upload } from "sfn";

export default class extends HttpController {

    @route.post("/upload")
    @upload("field1", "field2")
    upload(req: Request) {
        // The req.files property will carry the uploaded files,
        // each field may carry several files.
        console.log(req.files.field1[0]);
        console.log(req.files.field2[0]);
    }
}

Configure Uploading Options

import { HttpController, Request, Response, route, upload } from "sfn";

export default class extends HttpController {

    constructor(req: Request, res: Response) {
        super(req, res);

        // Set each field to carry no more than 5 files.
        this.uploadOptions.maxCount = 5;

        // Set the uploaded filename (extension exclusive) to be a random 
        // string.
        this.uploadOptions.filename = "random";
    }
}

The uploadOptions is an UploadOptions, which contains these properties:

The File State

The file state in the constructor and the route-binding method is different, in the constructor (also in the before() method), the file is in uploading state, while in the method, it's uploaded.

import { HttpController, Request, Response, UploadingFile } from "sfn";

export default class extends HttpController {

    constructor(req: Request, res: Response) {
        super(req, res);

        this.uploadOptions.filter = (file: UploadingFile) => {
            // Do not try to access `req.files` in the constructor, it's 
            // undefined because the file is not yet uploaded.
            // ...
        }
    }
}