hysagopt
v1.0.1
Published
hyeserat
Downloads
3
Readme
File Upload Handler
A simple Node.js package for handling file uploads.
Installation
Install the package using npm:
npm install file-upload-handler
Usage
const FileUploadHandler = require('file-upload-handler');
// Initialize FileUploadHandler with upload directory
const uploadDir = '/path/to/upload/directory';
const uploadHandler = new FileUploadHandler(uploadDir);
// Example usage
const file = {
data: 'file content', // Actual file content
name: 'example.txt' // Original file name
};
uploadHandler.handleUpload(file)
.then(result => {
if (result.success) {
console.log('File uploaded successfully:', result.fileName);
console.log('File path:', result.filePath);
} else {
console.error('Failed to upload file:', result.error);
}
})
.catch(error => {
console.error('Error uploading file:', error);
});
API
FileUploadHandler(uploadDir)
Creates a new instance of FileUploadHandler with the specified upload directory.
uploadDir
: The directory where uploaded files will be saved.
handleUpload(file)
Handles the upload of a file.
file
: An object containing the file data and name. Example:{ data: 'file content', name: 'example.txt' }
Returns a Promise that resolves with an object containing information about the upload:
success
: A boolean indicating whether the upload was successful.fileName
: The generated file name after upload.filePath
: The path to the uploaded file.
License
This project is licensed under the MIT License - see the LICENSE file for details.