Rails将文件上传到FTP服务器 [英] Rails upload file to ftp server

查看:137
本文介绍了Rails将文件上传到FTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 2.3.5和Ruby 1.8.6上试图找出如何让用户通过我的Rails应用程序将文件上传到不同机器上的FTP服务器。另外我的Rails应用程序将托管在Heroku上,这不利于将文件写入本地文件系统。

index.html.erb

 <%form_tag'/ ftp / upload',:method => :post,:multipart => true do%> 
< label for =file>要上传的文件< / label> <%= file_field_tag文件%>
<%= submit_tag'上传'%>
<%end%>

ftp_controller.rb

  require'net / ftp'​​

class FtpController< ApplicationController
def upload
file = params [:file]
ftp = Net :: FTP.new('remote-ftp-server')
ftp.login(user = ***),passwd =***)
ftp.putbinaryfile(file.read,File.basename(file.original_filename))
ftp.quit()
end

def index
end

end

目前我只是想让Rails应用程序在我的Windows笔记本电脑上工作。使用上面的代码,我收到了这个错误:

  Errno :: ENOENT in FtpController#upload 
没有这样的文件或目录-....后跟文件内容的转储

我试图上传一个CSV文件,如果这有什么区别。任何人都知道发生了什么?

解决方案

经过大量研究和敲门之后,我最终读到了putbinaryfile方法的源代码解决了putbinaryfile的局限性。这里是工作代码,替换此行

  ftp.putbinaryfile(file.read,File.basename(file.original_filename))

with

  ftp.storbinary(STOR+ file.original_filename,StringIO.new(file.read),Net :: FTP :: DEFAULT_BLOCKSIZE)

如果您想知道,STOR是一个原始的FTP命令,是的。我很惊讶这种情况不容易被Ruby标准库处理,但它肯定不是很明显需要做什么。



如果您的应用程序位于Heroku上,请添加以下行:

  ftp.passive = true 

Heroku的防火墙设置不支持FTP主动模式,也请确保您的FTP服务器支持被动模式。


I'm on Rails 2.3.5 and Ruby 1.8.6 and trying to figure out how to let a user upload a file to a FTP server on a different machine via my Rails app. Also my Rails app will be hosted on Heroku which doesn't facilitate the writing of files to the local filesystem.

index.html.erb

<% form_tag '/ftp/upload', :method => :post, :multipart => true do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag 'Upload' %>
<% end %>

ftp_controller.rb

require 'net/ftp'

class FtpController < ApplicationController
  def upload
    file = params[:file]
    ftp = Net::FTP.new('remote-ftp-server')
    ftp.login(user = "***", passwd = "***")
    ftp.putbinaryfile(file.read, File.basename(file.original_filename))
    ftp.quit()
  end

  def index
  end

end

Currently I'm just trying to get the Rails app to work on my Windows laptop. With the above code, I'm getting this error

Errno::ENOENT in FtpController#upload
No such file or directory -.... followed by a dump of the file contents

I'm trying to upload a CSV file if that makes any difference. Anyone knows what's going on?

解决方案

After much research and head banging, I ended up reading the source code for putbinaryfile method to figure out a workaround for the limitation of putbinaryfile. Here's the working code, replace this line

ftp.putbinaryfile(file.read, File.basename(file.original_filename))

with

ftp.storbinary("STOR " + file.original_filename, StringIO.new(file.read), Net::FTP::DEFAULT_BLOCKSIZE)

And in case you are wondering, STOR is a raw FTP command, yeah it came to that. I'm rather surprised this scenario isn't more easily handled by Ruby standard libraries, it certainly wasn't obvious what needed to be done.

And if your app is on Heroku, add this line

ftp.passive = true

Heroku's firewall setup does not allow for FTP active mode, also make sure that your FTP server supports passive mode.

这篇关于Rails将文件上传到FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆