Node.js+web TWAIN for Web document scanning and image uploading

Source: Internet
Author: User

With a combination of the dynamic Web TWAIN SDK and node. js, only a few lines of code can be implemented to control the scanner in the browser, get the image and upload it to the remote server.

Original:Document Imaging and uploading with Dynamic Web TWAIN and node. js

Download installation
    • Dynamic Web TWAIN Download

    • node. js Download

Creating a server from node. js

Create a Project catalog, open cmd.exe into the project catalog, install the following two node. js Modules:

NPM Install [email protected]npm install Express

Create Server.js, Initialize:

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

The static resources, compared to slices, CSS, etc., are loaded in:

App.use (Express.static (__dirname, '/public '));

To achieve cross-domain access, you need to add permissions to the header and, if not added, only local access:

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 (); });

Parse the data through formidable in the POST request:

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 the IP and port:

var server = App.listen, function () {var host = Server.address (). Address;    var port = server.address (). Port; Console.log (' Listening at http://%s:%s ', host, Port);})
Creating a client with dynamic WEB TWAIN

Create a Web page that contains a div and two buttons:

  

A copy of the resources directory under the Web TWAIN SDK installation directory is required here.

Add a few lines of code to scan:

function Acquireimage () {Dwobject.ifshowui = false;     Dwobject.selectsource ();     Dwobject.opensource (); Dwobject.acquireimage ();}

Here you can test whether the scan is working properly. Next, implement remote uploads:

Function btnupload_onclick ()  {       dwobject.httpport =  2014;       var currentpathname = unescape ( Location.pathname); // get current pathname in plain ascii        var currentpath = currentpathname.substring (0,  Currentpathname.lastindexof ("/")  + 1);       var  stractionpage = currentpath +  "Upload";       var  strhostip =  "localhost";  // modify the ip for cross-domain access        var sfun = function () {            alert (' successful ');        }, ffun  = function () {     &Nbsp;     alert (' failed ');       };        dwobject.httpuploadthroughpostex (        strhostip,       dwobject.currentimageindexinbuffer,        strActionPage,        "Test.jpg",        1,// JPEG       sFun, fFun        );}

Under test. Command line Start server:

Node Server.js

Open the http://localhost:2014 and you can play.

Source

Https://github.com/DynamsoftRD/nodejs-dwt

git clone https://github.com/DynamsoftRD/nodejs-dwt.git


Node.js+web TWAIN for Web document scanning and image uploading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.