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.常见的数据库管理 ...
随机推荐
- 学习总结 java基础
- java基础回顾(三)——HashMap与HashTable
public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable public cla ...
- [Hibernate 1]Hibernate的环境搭建
一.Hibernate是什么 直接使用JDBC操作数据库的步骤很繁琐,JDBC操作的是关系型数据库,而我们用JAVA开发程序,则使用面向对象的思想.Hibernate正是在这两种不同的模型之间建立关联 ...
- OpenGL函数解析之glLoadIdentity()
函数原型:void glLoadIdentity(void) 函数说明:调用glLoadIdentity()函数可以恢复初始坐标系,用一个4x4的单位矩阵来代替当前矩阵,实际上就是对当前矩阵进行初始化 ...
- poj2000
为了凑今天的数,大水题.不解释了,说来惭愧. #include <stdio.h> int main(){ int n; int i,cnt,j; int tot; while(~scan ...
- 007Linux在线升级yum
1.Linux下如何安装软件:利用rpm命令进行安装: 2.rpm优点:安装过程很简单,不需要做额外的配置逻辑,拿到安装包,通过rpm命令就可以安装: 3.rpm缺点: (1)需要自己四处去找和系统版 ...
- C puzzles详解
题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...
- Ubuntu工具:vi编辑器
Vi简介 Vi是“Visual interface”的简称,它在Linux上的地位就仿佛Edit程序在DOS上一样.它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且用户可以根据自己的需要对 ...
- jquery图片轮播,单张图片轮播时间不同
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- NSBundle UIImageView &UIButton
1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 模拟器应 ...