Skip to content

Upload multiple files

This instance is an example for demonstrations, see more about the configuration here

ts
const fupinstance = new FupNode({
  path: "uploads",
});

Function to upload multiple files

ts
async function uploadMultipleFiles(
  // Files bodies from the `frontend` client
  files: FileBody[],

  // File upload configuration settings
  options?: MultipleUploadSettings,

  // Middlewares to use in the files uploaded
  middlewares?: FupMiddleware[] | FupMiddleware
): Promise<string[]>; // Returns array of strings with the file names

To learn more about the FupMiddleware type and the use of middlewares see here

Type MultipleUploadSettings

note: The properties are very similar to those of SingleUploadSettings, and will apply to all uploaded files.

ts
type MultipleUploadSettings = {
  /* 
    Custom names for uploaded files the number of names 
    must correspond to the number of files uploaded.

    By default the file is saved with a name generated_
    with Date Now so as not to use `generateNamesByDate` = false 
  */
  names?: string[];

  // Used to generate the name of uploaded files using the current UTC time
  // By default it is activated
  generateNamesByDate?: boolean;

  // Used to generate the name of uploaded files with a unique UUID
  generateNamesByUUID?: boolean;

  // If enabled, use the original file name to save it.
  useOriginalFilenames?: boolean;

  // Maximum size that the file can be uploaded by default_
  // uses the one from the general configuration
  maxFilesSize?: number;

  // Maximum of files is permitted upload
  // By default uses the one from the general configuration
  maxFiles?: number;

  // File types allowed for uploading
  // By default any type of file is allowed to be uploaded ["*"]
  types?: string[];
};

Example using default configuration

ts
// By default the file name is generated using date now
const nameFiles = fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2]);

Example using custom names

note: Be careful when using this option because it can cause errors if you try to upload a file with the same name.

ts
const nameFiles = await fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2], {
  names: ["custom-name1.txt", "custom-name2.txt"],

  //It is false because by default a name generated by date now is used
  generateNamesByDate: false,
});

Example generating names by unique identifier UUID

ts
const nameFiles = await fupinstance.uploadMultipleFiles(bodyFile, {
  generateNamesByUUID: true,
});

Example generating name using the original file name

note: Be careful when using this option because it can cause errors if you try to upload a file with the same name.

ts
const nameFiles = await fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2], {
  useOriginalFilenames: true,
});

Example allowing only specific file types to be uploaded

note: If any MIME type defined in the types list is not valid an error will occur.

ts
// Defines that only plain text files and png images can be uploaded
const nameFiles = await fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2], {
  types: ["text/plain", "image/png"],
});
ts
// The * after the name defines that any type of image can be uploaded
const nameFiles = await fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2], {
  types: ["image/*"],
});
ts
/* 
  Using * already allows the upload of any type of file, 
  but its use is not necessary because it is used by default 
  if you do not assign types.
*/
const nameFiles = await fupinstance.uploadMultipleFiles([bodyFile1, bodyFile2], {
  types: ["*"],
});

Error handling

The errors from the uploadFile function are also used here.

About the error: Error the max files is exceeded.

  • message -> The number of files uploaded exceeds the maximum allowed of maxFiles!
  • maxFiles = It is the maximum number of files to upload defined.

About the error: The number of files uploaded not equal to number of names.

  • message -> The number of files uploaded not equal to number of names!