Document Imaging and Uploading With Dynamic Web TWAIN and Node.js

How to implement a simple Web application for document imaging and uploading with Dynamic Web TWAIN SDK (DWT) and Node.js?

nodejs_dwt

Download and Installation

Creating a Web Server with Node.js

Run your command line tool to install two Node.js modules:

npm install formidable@latest
npm install express

Create a file server.js, and make the initialization:

var formidable = require('formidable');
var util = require('util');
var express = require('express');
var fs = require('fs');
var app = express();

Load static resources, such as image files, css files and etc.

app.use(express.static(__dirname, '/public'));

For cross-domain access, you need to set permissions in header:

app.use(function(req, res, next) {  
    res.header("Access-Control-Allow-Origin", "*");   
    res.header("Access-Control-Allow-Methods","PUT, POST, GET, DELETE, OPTIONS");  
    res.header("Access-Control-Allow-Headers","X-Requested-With, content-type");  
    res.header("Access-Control-Allow-Credentials", true);  
    next();  
});

In the POST request, use formidable to extract file information:

app.post('/upload', function(req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        // console.log(util.inspect({
        //     fields: fields,
        //     files: files
        // }));

        fs.readFile(files.RemoteFile.path, function(err, data) {
        	// save file from temp dir to new dir
            var newPath = __dirname + "/uploads/" + files.RemoteFile.name;
            fs.writeFile(newPath, data, function(err) {
            	if (err) throw err;
            	console.log('file saved');	
                res.end();
            });
        });
    });
})

Set a port number and run the server:

var server = app.listen(2014, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('listening at http://%s:%s', host, port);
})

Read more >>

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com