# File Upload

# Introduction

ExpressWebJs provides a robust built-in file processor module for dealing with file uploads. Not only can you process and store uploaded files locally, you can also stream them directly to cloud services like Amazon S3, Cloudinary, or Google cloud storage. This module is fully configurable and you can adjust its behavior to your application requirements.

# Basic example

ExpressWebJs bodyparser automatically processes all the files for multipart/form-data requests. You can access the files by passing req.files into FileProcessor instance. FileProcessor exposes APIs like fieldName which returns an instance of the File class, or null if no file was uploaded.

import { FileProcessor } from "Elucidate/FileProcessor";

 public async upload(req: Request, res: Response) {
    const fileProcessor = new FileProcessor(req.files);
    const files = fileProcessor.fieldName("file");

    if(files && !Array.isArray(files)){
        await files.mv(`App/Storage/${files.name}`).catch((error) => {
          throw new Error(error);
        });
    }
    return this.response.OK(res, "file successfully uploaded");
 }

From the above example, you will notice that you can also accept multiple files. All you have to do is to loop through and save.

import { FileProcessor } from "Elucidate/FileProcessor";

 public async upload(req: Request, res: Response) {
    const fileProcessor = new FileProcessor(req.files);
    const files = fileProcessor.fieldName("file");

    if(files && Array.isArray(files)){
        for await (let file of files) {
            await file.mv(`App/Storage/${file.name}`).catch((error) => {
              throw new Error(error);
            });
        }
    }
    return this.response.OK(res, "file successfully uploaded");
 }

# Validating File

Files can be validated by specifying the rules for the file mimetype and the file size, while ExpressWebJs performs the validations implicitly.

import { FileProcessor } from "Elucidate/FileProcessor";

 public async upload(req: Request, res: Response) {
   const fileProcessor = new FileProcessor(req.files);
   const files = fileProcessor.fieldName("file");

   if(files && !Array.isArray(files)){
    const validation = await req.validate(files, {
        size: "required|min:2|max:626140",
        mimetype: ["required", { in: ["image/png", "image/jpg"] }],
    });

    if (!validation.success) return this.response.EXPECTATION_FAILED(res, {data: validation});

    await files.mv(`App/Storage/${files.name}`).catch((error) => {
        throw new Error(error);
    });
   }
   return this.response.OK(res, "file successfully uploaded");
 }

# File properties/methods

Below is the list of propertise in the file class.

# name

file name.

file.name;

# size

The file size is in bytes. The file size is only available when the file stream has been consumed.

file.size;

# data

A buffer representation of your file, returns empty buffer in case useTempFiles option was set to true.

file.data;

# encoding

Encoding type of the file

file.encoding;

# tempFilePath

A path to the temporary file in case useTempFiles option was set to true.

file.tempFilePath;

# truncated

A boolean that represents if the file is over the size limit.

file.truncated;

# mimetype

The mimetype of your file.

file.mimetype;

# md5

MD5 checksum of the uploaded file

file.md5;