使用Node.js,Express和Mongoose上传图像

编程入门 行业动态 更新时间:2024-10-21 20:24:32
本文介绍了使用Node.js,Express和Mongoose上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请考虑更多的最新信息,因为这些年来发生了变化!

由于许多新的节点.js库很快就被淘汰了,而且我想要使用最新版本的Node.js(v0.4.1),Express(1.0.7)和Mongoose(1.1.0)上传图像的例子相对较少, 。其他人如何做?

Since many new Node.js libraries are quickly being rendered obsolete and there are relatively few examples anyways I want to ask about uploading images using the latest versions of Node.js (v0.4.1), Express (1.0.7), and Mongoose (1.1.0). How have others done it?

我发现: github/felixge/node-formidable ,但我一般来说是上传图像,所以我想通过使用Node.js和Express来学习一般的东西和方法。

I've found: github/felixge/node-formidable, but I am new to uploading images in general so I want to learn general stuff and ways of doing so using Node.js and Express.

推荐答案

我会第一次回答我自己的问题。我从源头上发现了一个例子。请原谅不好的缩进。复制和粘贴时,我不知道如何正确缩进。代码直接来自Express multipart / form在

I'll answer my own question for the first time. I found an example straight from the source. Please forgive the poor indentation. I wasn't sure how to indent properly when copying and pasting. The code comes straight from Express multipart/form-data example on GitHub.

// Expose modules in ./support for demo purposes require.paths.unshift(__dirname + '/../../support'); /** * Module dependencies. */ var express = require('../../lib/express') , form = require('connect-form'); var app = express.createServer( // connect-form (github/visionmedia/connect-form) // middleware uses the formidable middleware to parse urlencoded // and multipart form data form({ keepExtensions: true }) ); app.get('/', function(req, res){ res.send('<form method="post" enctype="multipart/form-data">' + '<p>Image: <input type="file" name="image" /></p>' + '<p><input type="submit" value="Upload" /></p>' + '</form>'); }); app.post('/', function(req, res, next){ // connect-form adds the req.form object // we can (optionally) define onComplete, passing // the exception (if any) fields parsed, and files parsed req.formplete(function(err, fields, files){ if (err) { next(err); } else { console.log('\nuploaded %s to %s' , files.image.filename , files.image.path); res.redirect('back'); } }); // We can add listeners for several form // events such as "progress" req.form.on('progress', function(bytesReceived, bytesExpected){ var percent = (bytesReceived / bytesExpected * 100) | 0; process.stdout.write('Uploading: %' + percent + '\r'); }); }); app.listen(3000); console.log('Express app started on port 3000');

更多推荐

使用Node.js,Express和Mongoose上传图像

本文发布于:2023-11-22 19:22:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   上传   js   Node   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!