我知道以前已经多次询问过这个问题,而且我已经阅读了几乎所有关于这个主题的内容,即:
使用Node.js,Express和Mongoose上传图像
这是迄今为止我发现的最好的.我的问题是他们仍然不是很清楚,关于这一点在网上的文档很少,讨论似乎针对的是比我更先进的人.
所以我真的很喜欢它,如果有人可以请告诉我如何使用Mongoose,Express和AngularJS上传图像.我实际上正在使用MEAN fullstack.(这个发生器准确无误 - https://github.com/DaftMonk/generator-angular-fullstack)
AddController:
'use strict';
angular.module('lumicaApp')
.controller('ProjectAddCtrl', ['$scope', '$location', '$log', 'projectsModel', 'users', 'types', function ($scope, $location, $log, projectsModel, users, types) {
$scope.dismiss = function () {
$scope.$dismiss();
};
$scope.users = users;
$scope.types = types;
$scope.project = {
name: null,
type: null,
images: {
thumbnail: null // I want to add the uploaded images _id here to reference with mongoose populate.
},
users: null
};
$scope.save = function () {
$log.info($scope.project);
projectsModel.post($scope.project).then(function (project) { …
Run Code Online (Sandbox Code Playgroud) 是的,我知道使用Node.js,Express和Mongoose上传图像以及如何使用node.js将图像存储在mongodb中?但那些解决方案不是我想要达到的目标.我希望能够使用Schema将图像存储在db(Mongodb with Mongoose)的二进制文件中.
那么,你知道任何使用组合Node.js,Express和Mongodb的应用程序吗?最好的情况是,如果有这个组合+ Jade的任何应用程序,但我想这要求很多.
提前致谢!
我正在使用多部分表单数据从客户端上传图像文件.我想接收并使用node.js将其作为服务器端的文件写入.
<html>
<body>
<form action="url" method="post" enctype="multipart/form-data">
<input type="text" name="imageName">
<input type="file" name="sam">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的客户端代码.如何在服务器端处理此文件.
我用连接表单上传图片.但如果我使用它,它就不起作用bodyParser()
.反过来说,如果我不使用bodyParser,我无法上传文件?
我如何让他们一起玩?这是我的配置:
app.configure(function() {
app.register('.html', require('ejs'));
app.set('views', __dirname + '/../views');
app.set('view engine', 'html');
app.use(gzippo.staticGzip(__dirname + '/../public'),{ maxAge: 86400000 });
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(form({
keepExtensions: true,
uploadDir: __dirname + '/../tmp'
}));
app.use(express.cookieParser());
app.use(express.session({
secret: 'test',
cookie: { secure: true },
store: new MySQLSessionStore(client.database, client.user, client.password)
}));
app.use(expressValidator);
app.use(app.router);
app.use(express.csrf());
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Node w/express上传图像,我收到以下错误.我在这方面看到了一些问题,指出异步数据库调用是导致问题的原因.我想在理论上使用bodyParser,bodyParser应该只是通过我连接然后强大,所以我更喜欢不禁用bodyParser的解决方案.
就我而言,我没有进行数据库调用 - 我的所有代码都在下面复制.任何人都可以放弃的任何亮点都将非常感激.
500错误:分析器错误,在IncomingForm.write(/Users/me/Projects/Project/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:145:17)在IncomingMessage解析44个字节40.(/Users/me/Projects/Project/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:95:12)在IncomingMessage.emit(events.js:64:17)在HTTPParser.onBody( http.js:121:23)在Socket.ondata(http.js:1026:22)在Socket._onReadable(net.js:683:在IOWatcher.onReadable 27)作为回调(net.js:177:10 )
这是我的app.js文件(咖啡脚本):
express = require('express')
routes = require('./routes')
app = module.exports = express.createServer()
app.configure(() ->
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.bodyParser({
uploadDir: '/tmp/upload'
}))
app.use(express.methodOverride())
app.use(express.cookieParser())
app.use(app.router)
app.use(express.static(__dirname + '/public'))
)
app.configure('development', () ->
app.use(express.logger())
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
)
app.configure('test', () ->
app.use(express.logger())
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
)
app.configure('production', () ->
app.use(express.logger())
app.use(express.errorHandler())
)
app.get('/images/new', (req, res) ->
console.log("getting image form")
res.render('forms/image_upload', {title: 'Images'})
) …
Run Code Online (Sandbox Code Playgroud) 您好,我正在处理这个简单的表单,尝试使用 发送文件到我的 Nodejs 服务器FormData
,但由于某种原因 Node 从未收到它。另外,我怎样才能让节点在页面上发回一条确认消息,说明已收到文件。我做错了什么或错过了什么?请帮忙。先感谢您。这是我的代码。
超文本标记语言
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var fd = new FormData();
fd.append( 'file', input.files[0] );
$.ajax({
url: '/uploadfile',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<form enctype='multipart/form-data'>
<input type= "text" name = "theName"/>
<input type="file" id="file" name="file">
<input type="submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
服务器(Node.js)
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res){
res.sendfile('./public/html/form-file.html');
}); …
Run Code Online (Sandbox Code Playgroud)