有很多解决方案可以在 PHP 方面处理 Summernote文件上传,但我无法找到一个合适的解决方案那里显示了如何在Node.js中处理它。
我的Javascript
$(document).ready(function() {
// $('#summernote').summernote();
$('#summernote').summernote({
height: 590,
focus: true,
callbacks: {
onImageUpload : function(file, editor, welEditable) {
saveFile(file[0], editor, welEditable);
}
}
});
function saveFile(file, editor, welEditable){
console.log(file)
data = new FormData();
data.append("file", file);
$.ajax({
data: data,
type: "POST",
url: "/save/wiki",
cache: false,
contentType: false,
processData: false,
success: function(url) {
console.log(url)
editor.insertImage(welEditable, url);
}
});
}
});
app.js
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var path = require('path');
var app = express();
var jsonPrsr = bodyParser.json();
var urlencodedPrsr = bodyParser.urlencoded({ extended: false });
var routes = require('./routes')(app, jsonPrsr, urlencodedPrsr, multer);
var port = process.env.PORT || 1337;
app.listen(port, function(err){
if(err){
console.log("Server failed to start on port: "+port+". Description: ");
console.log(err);
}else console.log(port+'is ON')
});
routes.js
module.exports = function(app, jsonParser, urlencodedParser, multer) {
app.post('/save/wiki', urlencodedParser, function(req, res){
if( typeof req.body != 'undefined' )
console.log(req.body); // THIS RETURNS {} ie. empty
var file_name = req.body.file;
var upload,
upload_destination = './public';
storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, upload_destination);
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' +Date.now());
}
});
upload = multer({ storage : storage}).single(file_name);
upload(req,res,function(err) {
if(err)
res.send("Error uploading file.");
else
res.send("File is uploaded");
res.end();
});
});
}
我的问题是
console.log(req.body);
返回空对象{}。没有档案。
我想要
将Summernote编辑器中触发的文件上传到目录
'./public'
有谁能告诉我缺少什么?或者哪里出错了?
由于
答案 0 :(得分:1)
尝试this,这是上传文件的一个很好的工作示例。
var express = require('express'),
multer = require('multer')
var app = express()
app.use(multer({ dest: './uploads/'}))
app.get('/', function(req, res){
res.send('hello world');
});
app.post('/', function(req, res){
console.log(req.body) // form fields
console.log(req.files) // form files
res.status(204).end()
});
app.listen(3000);