在javascript中读取、删除、复制或重写csv上传文件mongoose mongodb
您好,我正在尝试读取数据库中上载的文件,读取后,我想删除文件中的重复项并重写为新文件,然后我想在我的网页上显示它。请问我该怎么做 这是我的密码 这是文件上传post和get路径在javascript中读取、删除、复制或重写csv上传文件mongoose mongodb,javascript,mongodb,csv,mongoose,file-upload,Javascript,Mongodb,Csv,Mongoose,File Upload,您好,我正在尝试读取数据库中上载的文件,读取后,我想删除文件中的重复项并重写为新文件,然后我想在我的网页上显示它。请问我该怎么做 这是我的密码 这是文件上传post和get路径 const multer = require('multer'); const uuid = require('uuid').v4; const router = express.Router(); const File = require('../models/file'); const path = require(
const multer = require('multer');
const uuid = require('uuid').v4;
const router = express.Router();
const File = require('../models/file');
const path = require('path');
const fs = require('fs');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads');
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname);
const id = uuid();
const filePath = `files/${id}${ext}`;
File.create({filePath})
.then(() => {
cb(null, filePath);
})
}
})
const upload = multer ({
storage: storage,
limits: {fieSize: 10000000000},
fileFilter: function (req, file, cb){
checkFileType(file, cb);
}
}).array('file-upload');
function checkFileType(file, cb) {
const filetypes = /csv/;
const extname = filetypes.test(path.extname(file.originalname).toLocaleLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname){
return cb(null, true);
}else{
cb('Error: CSV Files only');
}
};
router.get('/uploads', (req, res) => {
File.find({})
.then((files) => {
res.render('myProfile/myProfile', {files: files});
})
});
router.post('/uploads',(req, response) => {
upload(req, response, (error) => {
if(error){
response.render('myProfile/myProfile', {
msg: error
});
}else{
console.log(req.files);
if(req.files === undefined){
response.render('myProfile/myProfile', {
msg: 'Error: No File Selected'
});
}else{
response.render('myProfile/myProfile', {
msg: 'File Uploaded Successfully',
//file= `uploads/${req.file.filename}`
});
}
}
})
});
/**
* function checkFileType(file, callback){
const filetypes = /csv|json/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if(mimetype && extname){
return callback(null, true);
}else{
callback('Error: CSV or JSON files Only!');
}
}
*/
/**
* //downloading the file after harmoniszing
const input = document.getElementById('file');
const button = document.getElementById('share');
let objectURL;
input.addEventListener('change', function(){
if(objectURL){
URL.revokeObjectURL(objectURL);
}
const file = this.files[0];
objectURL = URL.createObjectURL(file);
button.download = file.name;
button.href = objectURL;
});
*/
module.exports = router;
这就是我的模式
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FileSchema = new Schema(
{
filePath:String
},
{
timestamps:String
},
{
filename:String,
}
);
module.exports = mongoose.model('File', FileSchema);
最后是ejs模板
<div id="uploadfile" class="input-group">
<%= typeof msg != 'undefined' ? msg : '' %>
<form action="/uploads" class="input-group mt-1 mb-2" id="upload" method="POST" enctype="multipart/form-data">
<input type="file" class="form-control" id="inputGroupFile03" name="file-upload" aria-describedby="inputGroupFileAddon03" aria-label="Upload" id="file" accept=".csv" multiple>
<button class="btn btn-outline-success" type="submit" id="inputGroupFileAddon03"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-upload" viewBox="0 0 16 16">
<path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
<path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708l3-3z"/>
</svg></button>
</form>
</div>
<div id="uploadfile">
<table class="table border border-success" id="uploadfile">
<thead>
<tr>
<th class="table-head border border-success">NAME</th>
<th class="table-head border border-success">DELETE</th>
<th class="table-head border border-success">SHARE</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
名称
删除
分享
我已成功上载文件路径并将其路径保存到我的数据库。然而,我坚持在阅读上传的文件部分
有人能帮忙吗
多谢各位