Takes the filepath table name columns names and your db file path that contains you configuration of ms sql and upload the file to ms sql table
npm install @shamigondal/csv-uploader
bash
npm install @shamigondal/csv-uploader
`
Also install all these packages
`bash
npm i fs path multer bodyparser express
`
index.html:
Create the required HTML files:
`html
CSV Uploader
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous">
integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
crossorigin="anonymous">
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous">
`
success.html
`html
File Upload Success
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous">
integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
crossorigin="anonymous">
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous">
`
db.js
create db.js file to connect with the ms sql Server.
Remember to to first authenticaiton at ms sql server, then you will get user and password.
`node.js
const sql = require('mssql');
const config = {
server: 'localhost',
user: '', // Your MS SQL Server username
password: '', // Your MS SQL Server password
database: 'testing', // Your database name
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
options: {
encrypt: true,
trustServerCertificate: true,
trustedConnection: true,
connectionTimeout: 30000,
},
};
const pool = new sql.ConnectionPool(config);
const connectToDatabase = async () => {
try {
await pool.connect();
console.log('Connected to the database!');
return pool;
} catch (error) {
console.error('Error connecting to the database:', error.message);
throw error;
}
};
module.exports = connectToDatabase;
`
app.js
creat app .js file first install the package
`bash
npm install @shamigondal/csv-uploader
`
Now write the following connectToDatabase
`node.js
const express = require('express')
const path = require('path')
const bodyparser = require('body-parser')
const multer = require('multer')
const {uploadCsv} = require('csv-uploader')
const connectToDatabase = require('./db');
const app = express();
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
const PORT = 4000 || 3000;
const localhost = http://localhost:
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, './uploads')
},
filename: (req, file, callback) => {
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
const uploads = multer({
storage: storage
})
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.post('/post-Csv-file', uploads.single('file'), async (req, res) => {
try {
console.log(req.file.path);
const tableName = 'users'; // Replace with your table name
const columnNames = ['userId', 'userName', 'userEmail', 'userPass']; // Replace with your column names
await uploadCsv(path.join(__dirname, 'uploads', req.file.filename), tableName, columnNames, connectToDatabase);
res.sendFile(__dirname + '/success.html');
} catch (error) {
console.error('Error processing CSV file:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(PORT, (req, res) => {
console.log("CSV Faker listening at PORT " + localhost + PORT)
})
``