save-file-npm
v1.0.0
Published
With this incredible package, managing file uploads has never been easier! You can now simply use the saveFile() method with your inputs and directory, and watch as it handles all the logic for you. The inputs can be of any type - file-object (image, PDF,
Downloads
232
Maintainers
Readme
Save File Functionality
This function allows you to save a file to a specified folder on the server. It accepts various input types including URLs, base64 strings, and file objects.
Installation
npm install save-file-npm
Live DEMO:
- Node JS - Stackblitz DEMO
Usage
saveFile(source, folder, fileName?, req?) Uploads a file from the given source to the specified folder. The fileName parameter is optional and can be used to set a custom name for the uploaded file. The req parameter is also optional and can be used to get the full URL of the uploaded file.
Parameters
The saveFile
function takes in the following parameters:
file
: This can be an object, string, or base64 data representing the file you want to save. It is a required parameter.folderName
: This is a string representing the name of the folder where the file will be saved. It is a required parameter.fileName
(optional): This is an optional parameter that allows you to set a custom file name for the saved file.req
(optional): This is an optional parameter that can be used to retrieve the current full file URL if needed. Examples
Here are some examples of how to use the saveFile function:
Import the "getColumnSum" function into your component or service by adding the following code:
const saveFile = require('save-file-npm');
const saveFile = require('save-file-npm');
// Upload a file from a URL to a folder
saveFile('https://pbs.twimg.com/profile_images/1410074091965403139/IqExKETZ_400x400.jpg', 'new_dir_1')
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
// Upload a file from a URL to a folder and get the full URL
saveFile('https://pbs.twimg.com/profile_images/1410074091965403139/IqExKETZ_400x400.jpg', 'new_dir_1', null, req)
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
// Upload a file from a base64-encoded string to a folder
saveFile(base64, 'new_dir_1')
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
// Upload a file from a file object to a folder
const fileObject = req.file;
saveFile(fileObject, 'new_dir_1')
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
// Upload a file from a file object to a folder and get the full URL
saveFile(fileObject, 'new_dir_2', null, req)
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
// Upload a file from a URL to a folder with a custom file name and get the full URL
saveFile('https://pbs.twimg.com/profile_images/1410074091965403139/IqExKETZ_400x400.jpg', 'new_dir_2', 'new-logo-name', req)
.then(result => {
console.log(result);
})
.catch(err => console.log(err));
Conclusion
This saveFile
function is a simple and effective way to save files to a server using a variety of input types. If you have any questions or issues using this function, please don't hesitate to reach out for support.