ExpressJS File Uploading – GridFS – MongoDB
n this blog post we will see how to handle multipart data/file uploading with expressjs. Save files to mongodb using GridFS and rending files.
To handle file uploads in express, i will use the library located at https://github.com/expressjs/multer
$ npm install multer --save |
In your express app.js file
var app = express(); app.use( '/uploads' , express. static (__dirname + "/uploads")); app.use(multer({dest: './uploads/' })) |
The above code catches all multipart data, fileuploads automatically and stores the file to ‘uploads/’ folder. So its super easy. So basically if you have a form tag, with its action pointed to a express route. Fileupload server handling is taken care automatically and all file move to ‘uploads’ folder.
Now let’s see how to save uploaded file to mongodb. Detailed explanation of using gridfs here
Suppose the file upload URL is http://127.0.0.1:3000/upload
var fs = require( 'fs' ); var mongoose = require( 'mongoose' ); router.all( '/upload' , function (req,res){ var dirname = require( 'path' ).dirname(__dirname); var filename = req.files.file.name; var path = req.files.file.path; var type = req.files.file.mimetype; var read_stream = fs.createReadStream(dirname + '/' + path); var conn = req.conn; var Grid = require( 'gridfs-stream' ); Grid.mongo = mongoose.mongo; var gfs = Grid(conn.db); var writestream = gfs.createWriteStream({ filename: filename }); read_stream.pipe(writestream); }); |
Now let’s see how to view image file uploaded in mongo
If URL to view files is http://127.0.0.1:3000/file/mongo_id
router.get( '/file/:id' , function (req,res){ var pic_id = req.param( 'id' ); var gfs = req.gfs; gfs.files.find({filename: pic_id}).toArray( function (err, files) { if (err) { res.json(err); } if (files.length > 0) { var mime = 'image/jpeg' ; res.set( 'Content-Type' , mime); var read_stream = gfs.createReadStream({filename: pic_id}); read_stream.pipe(res); } else { res.json( 'File Not Found' ); } }); }); |
ExpressJS File Uploading – GridFS – MongoDB的更多相关文章
- nginx+gridfs+mongodb 配置访问png图片显示无法加载问题
上传文件后,浏览器中请求:http://<nginx server ip>:<port>/gfs/<my file> 浏览器出现"无法打开页面" ...
- MongoDB GridFS 对图片进行增删改
using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS ...
- NodeJS+ExpressJS+SocketIO+MongoDB应用模板
OS:Win8.1 with update 关键字:NodeJS,ExpressJS,SocketIO,MongoDB. 1.源代码下载:https://github.com/ldlchina/ESM ...
- Mongodb GridFS——适合大小超过16MB的文件
一.概述 GridFS是基于mongodb存储引擎是实现的“分布式文件系统”,底层基于mongodb存储机制,和其他本地文件系统相比,它具备大数据存储的多个优点.GridFS适合存储超过16MB的大型 ...
- MongoDB C++ gridfs worked example
使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html #include <mongoc.h> #i ...
- MongoDB GridFS(命令行+php操作)
一.GridFS是什么 & 为什么需要它 我们知道目前MongoDB的BSON文件最大只能是16M,也就是说单个文档最多只能存储16M的数据,那么如果需要MongoDB存储超过16M的大文件该 ...
- mongodb高级操作及在Java企业级开发中的应用
Java连接mongoDB Java连接MongoDB需要驱动包,个人所用包为mongo-2.10.0.jar.可以在网上下载最新版本. package org.dennisit.mongodb.st ...
- mongoDB知识总结
官方说明文档:https://docs.mongodb.com/manual/mongo/ 1 NoSQL 简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库(相对于关系型数 ...
- MongoDB数据库详解
第1章 数据库管理系统 1.1 前言 01.数据的定义:文字.图像.地理位置信息(坐标.经纬度)等 02.数据库管理系统的定义:建立.存取和管理数据,保证数据安全和完整性的软件 03.常见的数据库管理 ...
随机推荐
- 学习总结 DML数据库增删改语句
insert into score t values('111','3-105',88)--插入一行数据 insert into score(sno,cno) values('111','3-105' ...
- 华为OJ平台——超长正整数相加
题目描述: 请设计一个算法完成两个超长正整数的加法. 输入 输入两个字符串数字 输出 输出相加后的结果,string型 样例输入 99999999999999999999999999999999999 ...
- 关于hook d3d在war3上绘图的几点疑问
学到了. 你得记住,com接口全是stdcall调用方式,不是thiscall,不要搞错了,不信,你看接口定义 因为com调用得兼容c调用,而c没有thiscall调用方式stdcall时,this指 ...
- 【drp 9】Servlet生命周期
一.基本概念 Servlet(Server Applet):全称Java Servlet,是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容.狭义的Servlet ...
- AX 获得当前Grid的数据源的记录行数
sysQuery::CountTotal();方法 eg: int lines = sysQuery::CountTotal( SalesTable_ds.QueryRun());
- 【MySQL】使用mysqlbinlog回滚
参考:http://wubx.net/?s=mysqlbinlog mysql官方的mysqlbinlog没有回滚的功能,淘宝大牛对官方代码进行了修改使之能够将binlog中的DML操作变成互逆的语句 ...
- iOS之UIAlertView的使用
UIAlertView: 1.普通使用: //普通的alert UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title&quo ...
- mysq 导入 导出
导入 use legou_seller set names utf8; source /Users/ldm/Desktop/ss.sql
- 003Linux网络配置
基于VMware中的Linux系统: 1.VMware提供了三种网络工作模式: (1)bridged(桥接模式) 桥接模式,顾名思义,得有桥,谁充当桥呢?当然是主机,安装了虚拟机的主机,充当的是虚拟机 ...
- 商业模拟游戏:<柠檬汁杰克>ios游戏源码
首先柠檬汁杰克是我个人的首个cocos2d-x开发的游戏,本人虽然混迹编程十几年从未开发过游戏,这是首例. 我选这个游戏因为逻辑比较简单,也是一款苹果上的经典游戏.开发中我用到了CocoStudio, ...