使用Node.js上传多张图片

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

我正在研究Node.js,并尝试处理多个图像.我正在使用以下代码上传单个图像,然后将路径以字符串格式保存到数据库.

I am working on Node.js and trying to handle multiple image. I am using following code to upload a single image and then saving the path in string format to the database.

var multiparty = require("multiparty"); var form = new multiparty.Form(); form.parse(req, function(err, fields, files) { var img = files.image[0]; var fs = require('fs'); fs.readFile(img.path, function(err, data) { var path = "/path/to/upload/" + img.originalFilename; fs.writeFile(path, data, function(error) { if (error) console.log(error); }); }); })

现在如何处理多张图片.任何帮助将不胜感激!

Now how to handle multiple image. Any help will be appreciated!

推荐答案

var express = require('express'), app = express(), formidable = require('formidable'), util = require('util'), fs = require('fs-extra'), bodyparser=require('body-parser'), qt = require('quickthumb'), path = require('path'); var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/db'); var Images = require('./model.js'); app.use(qt.static(__dirname + '/')); app.use(bodyparser()); app.set('view engine','ejs'); app.post('/upload',function (req, res){ var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { }); form.on('field',function(name,value){ }); form.on('end', function(fields, files) { for(var x in this.openedFiles) { //Images is my model var img = new Images(); var temp_path = this.openedFiles[x].path; /* The file name of the uploaded file */ var file_name = this.openedFiles[x].name; //console.log('file '+file_name); img.size = this.openedFiles[x].size; img.type = this.openedFiles[x].type; /* Location where we want to copy the uploaded file */ var new_location = 'uploads/'; console.log(img.nam=new_location+file_name); img.save(function(err,imgobj) { if (err) throw err; }); //to copy the file into a folder fs.copy(temp_path, new_location + file_name, function(err) { if (err) { console.log(err); } });//fscopy }//for loop });//form end res.send('Done!!'); });//post app.listen(3000); console.log('started server');

更多推荐

使用Node.js上传多张图片

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

发布评论

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

>www.elefans.com

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