-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridfs-stream-test.js
68 lines (54 loc) · 1.64 KB
/
gridfs-stream-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Created by KevinSo on 10/2/2014.
*/
//example of write a file
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
var fs = require('fs');
var raedStream=''
var buffer="";
var conn = mongoose.createConnection('127.0.0.1', 'test', 27017);
conn.once('open', function(){
var gfs = Grid(conn.db, mongoose.mongo);
//write file
/*
var source = fs.createReadStream('my_file.txt');
var target = gfs.createWriteStream({
filename: 'my_file.txt'
});
source.pipe(target);
//after the write is finished
target.on("close", function(){
// read file, buffering data as we go
raedStream = gfs.createReadStream({filename: "my_file.txt"});
raedStream.on("data", function(chunk){
buffer += chunk;
});
// dump contents to console when complete
raedStream.on("end", function(){
console.log("contents of file:\n\n", buffer);
});
});
*/
//example of read a file
raedStream = gfs.createReadStream({filename: "my_file.txt"});
raedStream.on("data", function(chunk){
buffer += chunk;
});
// dump contents to console when complete
raedStream.on("end", function(){
console.log("contents of file:\n\n", buffer);
});
// Checkk
gfs.exist({filename:"my_file.txt"}, function (err, found) {
if (err) return handleError(err);
found ? console.log('File exists') : console.log('File does not exist');
});
// remove file
/*
gfs.remove({filename:"my_file.txt"}, function(err){
if(err) return handleError(err);
console.log('success');
})
*/
});