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.常见的数据库管理 ...
随机推荐
- dom4j操作xml对象
// 获取Documen对象 public static Document getDocument(String path) throws Exception{ ...
- Java中可变长参数的使用及注意事项
在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定的参数,对于同一方法可以使用不同个数的参数调用,例如print("hello");print( ...
- 菜鸟学WEB开发 ASP.NET 5.0 1.0
在学习之初我要强调一点“微软要向跨平台开发”大举进军了,不管他能走多远,这是微软的必经之路. 一.学习流程: 创建ASP.NET APPLICATION 项目——项目结构——结构分析. 1.创建ASP ...
- 5 Ways to Learn and Remember Absolutely Anything
http://www.inc.com/quora/5-ways-to-learn-and-remember-absolutely-anything.html Start too early on th ...
- CSS渲染速度改善的十个方法与建议
由于不同浏览器对HTML标签的解释有差异,所以最终的网页效果在不同的浏览器中可能是不一样的,为了消除这方面的风险 一.*{} #zishu *{} 尽量避开 由于不同浏览器对HTML标签的解释有差异, ...
- CodeSmith 7.01破解下载
运行CodesmithKeyGenerator.exe,1.修改Prefix输入框的值为:CS70P-2.在主页面点Generate,生成注册码,填入激活码到code smith的Serial框中,( ...
- Sunglasses
It's hot this summer. It also reminds me of one case about sunglasses. She was new to this company a ...
- 【转载】linux中互斥尽量用mutex,不用semaphore
DEFINE_MUTEX是来自include/linux/mutex.h中的一个宏,用它可以定义一把互斥锁,在Linux内核中,其实是在2005年底才建立比较系统.完善的互斥锁机制,在那年冬天,来自R ...
- 爱之初体验---编译加载内核模块hello
1. hello.c #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h ...
- 移植u-boot-1.1.6(原创)
#u-boot:u-boot-1.1.6#server:ubuntu 12.04#gcc: arm-linux-gcc -4.3.2# 一.建立单板1> /board/smdk2410 : cp ...