Recently looked at Ruby on rails and tried to integrate dynamic WEB Twain into Ruby on Rails. Here's how to do file uploads in rails with a few lines of code.
Reference original: How to Load, Scan and Upload Files with Ruby on Rails
Desmond Shaw
Translation: Yushulx
Software Installation
Dynamic Web TWAIN 11.1
Ruby 2.1.7
Ruby Development Kit
Don't choose Ruby 2.2 on Windows, or you'll get an error when running Rails server:
Nokogiri not supported, details can be read https://github.com/sparklemotion/nokogiri/issues/1256.
Basic steps for Rails to create a project
Installing rails:
Gem Install Rails
To create an app:
Rails New DWT
CD to DWT
Start the service
Rails Server
Visit http://localhost:3000
Rails integrated Dynamic WEB Twain upload file
Create a Controller
Rails Generate controller Twainscanning Home
Copy the < Dynamic Web TWAIN directory >\resources to < Rails Project >\public\resources.
Open < Rails Project >\app\views\twainscanning\home.html.erb Add the following code:
Open < Rails Project >\app\controller\application_controler.rb commented out:
Class Applicationcontroller < Actioncontroller::base # Prevent CSRF attacks by raising an exception. # for APIs, the want to use:null_session instead. #protect_from_forgery with:: Exceptionend
Open < Rails Project >\config\routes.rb Add mappings:
Rails.application.routes.draw do get ' twainscanning/home ' root ' twainscanning#home ' post ' upload/' = ' twainscanning #upload ' End
Open < Rails Project >\app\controller\twainscanning_controller.rb Add File Upload code:
class twainscanningcontroller < applicationcontroller def home end def upload uploaded_io = params[:RemoteFile] upload_dir = Rails.root.join (' Public ', ' upload ') unless dir.exist? (Upload_dir) dir.mkdir (upload_dir) end file.open (Rails.root.join (' Public ', ' upload ', uploaded_io.original_filename), ' WB ') do |file| file.write (uploaded_io.read) end respond_to do |format| format.html.any { render text: "successfully uploaded!"} end endend
To run the service:
Source
Https://github.com/dynamsoftsamples/dwt-ruby-on-rails
Ruby on Rails server file upload