Ruby on Rails server file upload

Source: Internet
Author: User
Tags ruby on rails

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
    1. Installing rails:

      Gem Install Rails
    2. To create an app:

      Rails New DWT
    3. CD to DWT

    4. Start the service

      Rails Server
    5. 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

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.