У меня есть разные формы ввода для загрузки на одной странице о том, как загрузить с помощью мультера?
app.js :
const multer = require('multer');
const fileStroge = multer.diskStorage({
destination: (req, file,cb)=>{
cb(null, 'upload');
},
filename: (req, file, cb) =>{
cb(null, file.originalname);
}
});
const fileFilter = (req, file, cb)=>{
if(file.mimetype == 'application/octet-stream'){
cb(null,true);
}else{
cb(null,false);
}
};
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extented: true}));
app.use(multer({ storage: fileStroge, fileFilter: fileFilter}).single('filename'));
// app.use(multer({ storage: fileStroge, fileFilter: fileFilter}).single('filenameS'));
ниже мой js файл
user.js:
exports.uploadedData = (req, res)=>{
const file = req.file;
// console.log(file);
if( file == undefined){
res.send("you can upload excel sheet only");
}
readXlsxFile(req.file.path).then((rows) =>{
var database = mysql.createConnection(
{ host: 'localhost', user: 'root', password: 'ilensys@123', database: 'harness_db'}
);
var query = database.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT IGNORE INTO harnes_detials (id, item_des, harness_drw_pin, harness_type, harness_conn_type, mating_crimp, design_volt, connectors_mating_mfg, mating_connector_part_no, design_current, gauge, connector_type, connect_gender, no_pins, no_rows, connectors, manufacture, contact_rating, voltage_rating, maximum_temp, connector_orientation, d_connector_type, d_connector_gender, d_mating_connector_part_no, d_connector_mating, d_connectors_mating_mfg, d_no_pins, d_no_rows,d_connector, d_manufacture, d_contact_rating, d_voltage_rating, d_maximum_temp, d_connector_orientation, d_guage, d_mating_crimp, createdAt, updatedAt) VALUES ? ";
var newSql = "LOAD DATA INFILE 'req.file.path' INTO TABLE harnes_detials";
var values = rows.slice(1);
// console.log(values);
database.query(sql, [values], function (err, result) {
// console.log(result);
if(err){
console.log(err);
}else{
res.render("success");
console.log( "row inserted: "+ result.affectedRows);
}
});
});
});
};
exports.uploadedSourceData = (req, res)=>{
const file = req.file;
// console.log(file);
if( file == undefined){
res.send("you can upload excel sheet only");
}
readXlsxFile(req.file.path).then((rows) =>{
var database = mysql.createConnection(
{ host: 'localhost', user: 'root', password: 'ilensys@123', database: 'harness_db'}
);
var query = database.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT IGNORE INTO matingsources (id, mating_source_connectors,connector_type, source_connectors, gauge) VALUES ? ";
var newSql = "LOAD DATA INFILE 'req.file.path' INTO TABLE matingsources";
var values = rows.slice(1);
console.log(values);
database.query(sql, [values], function (err, result) {
console.log(result);
if(err){
console.log(err);
}else{
res.render("success");
console.log( "row inserted: "+ result.affectedRows);
}
});
});
});
};
ниже мой e js файл с входами:
<form class="needs-validation" action="/uploadData" method="post" enctype="multipart/form-data" novalidate>
<div class="custom-file mb-3">
<input type="file" class="custom-select custom-file-input" id="filename" name="filename" required>
<label class="custom-file-label" for="customFile">Choose file</label>
<div class="valid-feedback">Looks Good!</div>
<div class="invalid-feedback">need to upload a file!</div>
</div>
<div class="mt-3 text-center">
<button type="submit" class="btn btn-primary">Upload</button>
<a class="btn btn-secondary" href="javascript:history.back()" role="button">Cancel</a>
</form>
<form class="needs-validation" action="/uploadSourceData" method="post" enctype="multipart/form-data" novalidate>
<div class="custom-file mb-3">
<input type="file" class="custom-select custom-file-input" id="filenameS" name="filenameS" required>
<label class="custom-file-label" for="customFile">Choose file</label>
<div class="valid-feedback">Looks Good!</div>
<div class="invalid-feedback">need to upload a file!</div>
</div>
<div class="mt-3 text-center">
<button type="submit" class="btn btn-primary">Upload</button>
<a class="btn btn-secondary" href="javascript:history.back()" role="button">Cancel</a>
</form>
он отлично работает с первой опцией загрузки ввода, но при загрузке со второго входа с другого входа он говорит:
multererror: unexpected field
но когда я прокомментируйте альтернативно, это работает для обоих:
app.use(multer({ storage: fileStroge, fileFilter: fileFilter}).single('filename'));
// app.use(multer({ storage: fileStroge, fileFilter: fileFilter}).single('filenameS'));
но как работать с двумя различными загрузками входных данных на одной странице ?? пожалуйста помогите ??