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

Last Updated on 2018-10-08

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:
[javascript]npm install formidable@latest
npm install express[/javascript]
Create a file server.js, and make the initialization:
[javascript]var formidable = require(‘formidable’);
var util = require(‘util’);
var express = require(‘express’);
var fs = require(‘fs’);
var app = express();[/javascript]
Load static resources, such as image files, css files and etc.
[javascript]app.use(express.static(__dirname, ‘/public’));[/javascript]
For cross-domain access, you need to set permissions in header:
[javascript]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();
});[/javascript]
In the POST request, use formidable to extract file information:
[javascript]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();
});
});
});
})[/javascript]
Set a port number and run the server:
[javascript]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);
})[/javascript]

Read more >>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com