Ruby on Rails服务器文件上传

最近看了下ruby on rails,试着把Dynamic Web TWAIN集成到ruby on rails中。这里分享下如何在rails中用几行代码搞定文件上传。

参考原文:How to Load, Scan and Upload Files with Ruby on Rails

作者:Desmond Shaw

翻译:yushulx

软件安装

在Windows上不要选择Ruby 2.2,不然在运行rails server的时候会报错:

nokogiri不支持,详情可以阅读https://github.com/sparklemotion/nokogiri/issues/1256

Rails创建工程的基本步骤

  1. 安装rails: 

    ?
    1
    gem install rails
  2. 创建应用: 

    ?
    1
    rails  new  dwt
  3. cd到dwt

  4. 启动服务

    ?
    1
    rails server
  5. 访问http://localhost:3000

Rails集成Dynamic Web TWAIN上传文件

创建controller

?
1
rails generate controller twainscanning home

< Dynamic Web TWAIN directory >\Resources拷贝到< Rails Project >\public\Resources

打开< Rails Project >\app\views\twainscanning\home.html.erb添加下面的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<html>
  
<head>
   <title> DWT  with Ruby</title>
   <script type= "text/javascript"  src= "Resources/dynamsoft.webtwain.initiate.js" ></script>
   <script type= "text/javascript"  src= "Resources/dynamsoft.webtwain.config.js" ></script>
   <style>
     h1 {
       font-size: 2em;
       font-weight: bold;
       color:  #777777;
       text-align: center
     }
     table {
       margin: auto;
     }
   </style>
</head>
  
<body>
   <h1>
             DWT  with Ruby
   </h1>
   <table>
     <tr>
       <td>
         <!-- dwtcontrolContainer is the default div id  for  Dynamic Web  TWAIN  control.
                    If you need to rename the id, you should also change the id  in  dynamsoft.webtwain.config.js accordingly. -->
         <div id= "dwtcontrolContainer" ></div>
       </td>
     </tr>
  
     <tr>
       <td>
         <input type= "button"  value= "Load Image"  onclick= "btnLoad_onclick();"  />
         <input type= "button"  value= "Scan Image"  onclick= "AcquireImage();"  />
         <input id= "btnUpload"  type= "button"  value= "Upload Image"  onclick= "btnUpload_onclick()" >
       </td>
     </tr>
   </table>
  
   <!--Custom script goes here-->
   <script type= "text/javascript" >
     Dynamsoft.WebTwainEnv.RegisterEvent( 'OnWebTwainReady' , Dynamsoft_OnReady);
     var DWObject;
  
     function Dynamsoft_OnReady() {
       DWObject = Dynamsoft.WebTwainEnv.GetWebTwain( 'dwtcontrolContainer' ); // Get the Dynamic Web  TWAIN  object that is embeded  in  the div with id  'dwtcontrolContainer'
       DWObject.Width =  480 ; // Set the width of the Dynamic Web  TWAIN  Object
       DWObject.Height =  640 ; // Set the height of the Dynamic Web  TWAIN  Object
     }
  
     function btnLoad_onclick() {
       var OnSuccess = function() {};
  
       var OnFailure = function(errorCode, errorString) {};
  
       DWObject.IfShowFileDialog =  true ;
       DWObject.LoadImageEx( "" , EnumDWT_ImageType. IT_ALL , OnSuccess, OnFailure);
     }
  
     function AcquireImage() {
       if  (DWObject) {
         DWObject.IfShowUI =  false ;
         DWObject.IfDisableSourceAfterAcquire =  true ; // Scanner source will be disabled/closed automatically after the scan.
         DWObject.SelectSource(); // Select a Data Source (a device like scanner) from the Data Source Manager.
         DWObject.OpenSource(); // Open the source. You can set resolution, pixel type, etc. after this method. Please refer to the sample  'Scan'  ->  'Custom Scan'  for  more info.
         DWObject.AcquireImage(); // Acquire image(s) from the Data Source. Please  NOTE  this is a asynchronous method. In other words, it doesn't wait  for  the Data Source to come back.
       }
     }
  
     function btnUpload_onclick() {
       DWObject.HTTPPort =  3000 ;
       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" ; // server  IP  e.g.  192 . 168 . 8 . 84
  
       var OnSuccess = function(httpResponse) {
         alert( "Succesfully uploaded" );
       };
  
       var OnFailure = function(errorCode, errorString, httpResponse) {
         alert(httpResponse);
       };
  
       var date =  new  Date();
       DWObject.HTTPUploadThroughPostEx(
         strHostIP,
         DWObject.CurrentImageIndexInBuffer,
         strActionPage,
         date.getTime() +  ".jpg" ,
         1 , //  JPEG
         OnSuccess, OnFailure
       );
     }
  
   </script>
  
</body>
  
</html>

打开< Rails Project >\app\controller\application_controler.rb注释掉:

?
1
2
3
4
5
class  ApplicationController < ActionController::Base
   # Prevent CSRF attacks by raising an exception.
   # For APIs, you may want to use :null_session instead.
   #protect_from_forgery with: :exception
end

打开< Rails Project >\config\routes.rb 添加映射:

?
1
2
3
4
5
Rails.application.routes.draw  do
   get  'twainscanning/home'
   root  'twainscanning#home'
   post  'upload/'  =>  'twainscanning#upload'
end

打开< Rails Project >\app\controller\twainscanning_controller.rb添加文件上传代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
  
   end
end

运行服务:

源码

https://github.com/dynamsoftsamples/dwt-ruby-on-rails

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PHP和Ruby都是流行的服务器端编程语言,它们之间有很多共同点,但也有一些明显的不同。PHP在拥有更强大的语法功能和更广泛的支持方面具有优势,而Ruby则拥有更简单的语法和更容易理解的编程架构。另外,PHP的性能比Ruby要高得多,可以更快地处理大量的数据。 ### 回答2: PHP对比Ruby有以下几个优势: 1. 社区支持:PHP拥有庞大且活跃的开发者社区,有丰富的文档、教程和扩展库。这使得在PHP中寻找解决方案更加容易,开发者能够更快速地获取帮助和支持。 2. 速度:PHP在处理大量并发请求时表现出色,尤其是在与Apache、Nginx等Web服务器配合使用时。PHP的运行速度相对较快,适用于处理高流量的Web应用程序。 3. 平台支持:PHP能够运行在多种操作系统,包括Windows、Linux、Unix等,这使得它具有更广泛的适用性和可移植性。 4. 市场需求:由于PHP的流行度和广泛应用,许多企业和组织都在寻找具备PHP开发技能的人才。因此,精通PHP开发可以为开发者提供更多的就业机会和职业发展空间。 5. 可使用的框架:PHP拥有多个流行的框架,如Laravel、Symfony、CodeIgniter等。这些框架提供了经过验证的架构和内置的功能,帮助开发者更快速、更高效地构建Web应用程序。 6.易于学习和上手:相对于某些其他语言来说,PHP语法较为简单和直观,易于学习和上手。这使得初学者能够快速进入PHP开发领域,并且能够快速实现自己的项目。 需要注意的是,虽然PHP在某些方面可能具有优势,但Ruby也有自己的独特之处和优势,例如它的优雅性、可读性和强大的面向对象编程能力。因此,选择使用哪种语言还需要综合考虑项目需求、团队技能和个人偏好等因素。 ### 回答3: PHP和Ruby是两种常用的服务器端编程语言,各自都有自己的优势和适用场景。 首先,PHP有比Ruby更广泛的使用范围。PHP在Web开发领域有着较长的历史,因此有着更多的开发资源和社区支持。许多知名网站和框架,如WordPress、Drupal和Laravel等,都是使用PHP开发的。由于PHP的流行,很容易找到PHP开发者和相应的教程、文档等资源。 其次,PHP有更高的性能。PHP是一种被广泛优化和调优的语言,它的执行速度相对较快,尤其适用于处理大量并发请求和高负载的情况。此外,PHP的语法简洁易学,开发效率相对较高。 此外,PHP拥有更多的扩展和库。PHP的生态系统非常丰富,有着大量的第三方扩展和库,可以轻松地连接数据库、处理图像、发送邮件等。这些扩展和库提供了更多的功能和灵活性。 与此相比,Ruby在某些方面也有其独特的优势。Ruby有着更优雅和人性化的语法,尤其适合写可读性强的代码。Ruby也有一个活跃的社区,并且致力于开发优秀的Web框架,如Ruby on Rails。 综上所述,PHP和Ruby各自都有自己的优势和适用场景。如果需要广泛的使用范围、高性能和丰富的扩展支持,那么PHP是一个不错的选择。如果更注重代码的可读性和开发效率,以及更喜欢优雅的语法,那么Ruby可能更合适。最终的选择应取决于具体的项目需求和个人偏好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值