Function Signature
Description
Stores a file in the specified directory on the server. The function handles file extension extraction, directory creation, and path safety validation automatically.Parameters
The file object to store. Must contain:
name: string - Original filenamecontent: string - Data URL with base64-encoded file contentsize: string - File sizetype: string - MIME typelastModified: string - Last modified timestamp
Controls the filename of the stored file:When a number:
- Generates a unique random ID with the specified length
- Automatically appends the correct file extension
- Example:
8generates something likeaBcD1234.png
- Uses the provided string as the filename
- If no extension is provided, automatically appends the correct one
- If the extension matches the file type, uses as-is
- If the extension doesn’t match, warns and replaces with correct extension
The folder path where the file should be stored, relative to the configured mount point.
- Use forward slashes for subdirectories (e.g.,
/userFilesor/uploads/images) - Defaults to the root of the mount directory if not provided
- Path is automatically normalized and validated for security
Return Value
Returns the final filename of the stored file in the format
{filename}.{extension}Behavior Details
File Extension Handling
The function intelligently handles file extensions:- Extracts extension from original filename if it contains a dot
- Falls back to MIME type if no extension in filename
- Sanitizes extension by removing non-alphanumeric characters
- Automatically appends or corrects the extension based on the parameter type
Filename Generation
Using a number (recommended for user uploads):Directory Creation
The function automatically:- Creates the target directory if it doesn’t exist (recursive creation)
- Validates that the path is within the configured mount point
- Prevents path traversal attacks
- Provides detailed error messages for EEXIST and ENOTDIR errors
Examples
Basic Usage with Auto-Generated Filename
Multiple File Upload
Custom Filename
Organized Storage Structure
Error Handling
Common Errors
Mount not configured:fileStorage.mount to your nuxt.config.ts
EEXIST - File exists where directory expected:
See Also
- parseDataUrl - Extract binary data and extension from data URLs
- getFileLocally - Get the file path of a stored file
- deleteFile - Delete a stored file
- retrieveFileLocally - Stream a file to the client