Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Need to update this example to work with the latest versions of everything. #2

Merged
merged 2 commits into from almost 2 years ago

2 participants

Travis Tidwell Anatoliy Chakkaev
Travis Tidwell

I was having some issues getting this example to work. I had to make the following changes to get it working with the latest versions of everything. I also added an error if the directory doesn't exist.

Thanks for adding this example. It was very helpful.

Travis.

Anatoliy Chakkaev

Thank you Travis!

Anatoliy Chakkaev anatoliychakkaev merged commit 931cfd5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
15 app/controllers/files_controller.js
... ... @@ -1,6 +1,6 @@
1 1 load('application');
2 2
3   -before(loadFile, {only: ['show', 'edit', 'update', 'destroy']});
  3 +before(loadFile, {only: ['create', 'show', 'edit', 'update', 'destroy']});
4 4
5 5 action('new', function () {
6 6 this.file = new File;
@@ -10,7 +10,7 @@ action('new', function () {
10 10
11 11 action('create', function () {
12 12 this.file = new File();
13   - var tmpFile = req.form.files.file;
  13 + var tmpFile = req.files.file;
14 14 this.file.upload(tmpFile.name, tmpFile.path, function (err) {
15 15 if (err) {
16 16 console.log(err);
@@ -26,9 +26,14 @@ action('create', function () {
26 26
27 27 action('index', function () {
28 28 File.find(function (err, files) {
29   - this.files = files;
30   - this.title = 'Files index';
31   - render();
  29 + if (err) {
  30 + send(err);
  31 + }
  32 + else {
  33 + this.files = files;
  34 + this.title = 'Files index';
  35 + render();
  36 + }
32 37 }.bind(this));
33 38 });
34 39
24 app/models/file.js
@@ -4,13 +4,23 @@ function File(name) {
4 4 this.id = this.name = name;
5 5 }
6 6
  7 +// The directory to upload files too.
  8 +File.directory = app.root + '/data';
  9 +
7 10 File.find = function (cb) {
8 11 var files = [];
9   - fs.readdir(app.root + '/data', function (err, fileNames) {
10   - fileNames.forEach(function (file) {
11   - files.push(new File(file));
12   - });
13   - cb(err, files);
  12 + fs.exists(File.directory, function(exists) {
  13 + if (exists) {
  14 + fs.readdir(File.directory, function (err, fileNames) {
  15 + fileNames.forEach(function (file) {
  16 + files.push(new File(file));
  17 + });
  18 + cb(err, files);
  19 + });
  20 + }
  21 + else {
  22 + cb('You must create the directory ' + File.directory + '.');
  23 + }
14 24 });
15 25 };
16 26
@@ -23,7 +33,7 @@ File.prototype.remove = function (cb) {
23 33 };
24 34
25 35 File.prototype.filename = function () {
26   - return app.root + '/data/' + this.name;
  36 + return File.directory + '/' + this.name;
27 37 };
28 38
29 39 File.prototype.upload = function (name, path, cb) {
@@ -37,4 +47,4 @@ File.prototype.rename = function (name, cb) {
37 47 fs.rename(oldPath, this.filename(), cb);
38 48 };
39 49
40   -export('File', File);
  50 +module.exports = File
6 config/environment.js
... ... @@ -1,16 +1,14 @@
1 1 var express = require('express');
2   -var form = require('connect-form-sync');
3   -
4 2 app.configure(function(){
5 3 var cwd = process.cwd();
6 4 app.use(express.static(cwd + '/public', {maxAge: 86400000}));
7 5 app.set('views', cwd + '/app/views');
8 6 app.set('view engine', 'ejs');
  7 + app.set('view options', {complexNames: true});
9 8 app.set('jsDirectory', '/javascripts/');
10 9 app.set('cssDirectory', '/stylesheets/');
11   - app.use(form({ keepExtensions: true })); // https://github.com/anatoliychakkaev/connect-form-sync
12 10 app.use(express.bodyParser());
13   - app.use(express.cookieParser());
  11 + app.use(express.cookieParser('secret'));
14 12 app.use(express.session({secret: 'secret'}));
15 13 app.use(express.methodOverride());
16 14 app.use(app.router);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.