javascript - 阅读,删除重复在javascript中重写csv上传的文件mongoose mongodb
问题描述
你好,我正在尝试读取我的数据库中上传的文件,读取后我想删除文件中的重复项并重写为一个新文件,然后我想在我的网页上显示它。请问我该怎么做?
这是我的代码
这是文件上传帖子和获取路线
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>
我已成功上传文件路径并将其路径保存到我的数据库。但是我坚持阅读上传的文件部分。
请问谁能帮忙
谢谢你
解决方案
推荐阅读
- django - Django - 如何在模板中获取长度和计数命令?(模板中的查询集)
- javascript - Sequelize 仅获取自己的属性,忽略包含的实例
- android - 是否有任何相当于 iOS UIAccessibilityTraits 的 Android 设备?
- jenkins - 我如何在 Jenkins 管道中使用管道脚本文件(管道选项卡)
- r - 在 R 中使用 for 循环
- python - 按 ID 对 pandas 行进行分组,为时间条目创建新列
- java - 在条件评估期间是否可以获取 bean 名称?
- python - SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1076)
- angular - 如何使用打字稿客户端(Signalr Core)将标头传递给信号器集线器
- html - 电子邮件签名模板中使用的奇怪 CSS 类