Skip to content

Commit

Permalink
Start #25: Read imported file into browser
Browse files Browse the repository at this point in the history
Add a conditional to the controller to handle POST requests.  For now,
this reads the file into a variable and then sends it back to the
browser.  We'll do something different in the next commit.  Add a POST
route pointing to the controller.  Add a form to the front page to
receive the file for import (I'll have to move this, since it's right
above the search bar now).
  • Loading branch information
cecilia-donnelly committed Aug 4, 2015
1 parent f508029 commit b34aeba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/controllers/core.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@
* Module dependencies.
*/
exports.index = function(req, res) {
res.render('index', {
user: req.user || null,
request: req
});
var qs = require('querystring');
if (req.method == "POST"){
var body = '';
req.on('data', function (data) {
body += data;
if (body.length > 1e6) {
req.connection.destroy();
}
});
req.on('end', function () {
// naming this "file_data" because I assume that we will
// only be POST-ing imported files. This may change in the
// future.
var file_data = qs.parse(body);
res.send(file_data);
});
}
else{
res.render('index', {
user: req.user || null,
request: req
});
}
};
1 change: 1 addition & 0 deletions app/routes/core.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = function(app) {
// Root routing
var core = require('../../app/controllers/core.server.controller');
app.route('/').get(core.index);
app.route('/upload').post(core.index);
};
3 changes: 3 additions & 0 deletions app/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ html(lang="en")
img(src='img/OpenHMIS-logo.jpg')
div#search
h1 Client Search
form(action="/upload", method="post", enctype="multipart/form-data")
input#import_file(type='file' name="import.csv")
button#importAll(type='submit')
form#searchForm
div.form-group
input#searchField.form-control(name='searchField', type='text', placeholder='search by first or last name')
Expand Down
2 changes: 2 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ $(function() {
var revertText = "Revert Changes";
var backText = "Back to Results";
var exportAllText = "Example Export -- All Clients and Enrollments";
var importAllText = "Example Import";

/*
* Takes a user-entered string and returns the number of matching
Expand Down Expand Up @@ -309,6 +310,7 @@ $(function() {
$("#searchForm #results").empty();
$("#addNewClient").text(noCaveatText);
$("#exportAll").text(exportAllText);
$("#importAll").text(importAllText);
$("#searchForm #addNewClient").prop("disabled", true);
}
$("#search").css("display", "block");
Expand Down

0 comments on commit b34aeba

Please sign in to comment.