<!DOCTYPE html>
<html>
<body>
<form action = "/submit"
method = "post">
Select a file: <input type="file" name="file">
<input type="submit">
</form>
</body>
</html>
非常简单,网页会询问csv文件,然后用户按提交。然后,重点是在/ submit页面中显示该csv文件的内容。
节点js
var http = require('http');
var express= require('express')
var fs = require('fs');
var util = require('util');
var app= express();
var multer = require('multer');
//var bodyParser = require('body-parser');
//var dispatcher = require('httpdispatcher');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res){
var html = fs.readFileSync('index2.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});
app.post('/submit', upload.single('File'), function(req,res) {
});
//Listening to computer's IP address
app.listen(3000, "192.168.0.172");
console.log('Listening at 192.168.0.172:3000')
我创建了一个接收csv文件并解析它的辅助程序。我希望这个程序在客户端上传的文件上运行。
var columns = ["Parameter", "Address", "Data Size"];
require("csv-to-array")({
file: "parse1.txt",
columns: columns
}, function (err, array) {
console.log(err || array);
});
如何在节点js程序的post函数中使用上面的代码?
答案 0 :(得分:1)
此代码应该完成工作
var upload = multer({ dest: 'uploads/' })
app.post('/submit', upload.single('file'), function(req,res,next) {
fs.readFile(req.file.path, 'utf-8', function(err,data){
if(err){
res.end(err);
}
res.end(data);
});
});
并将其添加到.html
<form action="/submit" method="post" enctype='multipart/form-data'>