由于Heroku不允许将动态文件保存到磁盘,我遇到了一个困境,希望您能帮助我克服困难.我有一个我可以在RAM中创建的文本文件.问题是,我找不到一个gem或函数,这将允许我将文件流传输到另一个FTP服务器.我使用的Net / FTP宝石要求我首先将文件保存到磁盘.有什么建议么?
ftp = Net::FTP.new(domain) ftp.passive = true ftp.login(username,password) ftp.chdir(path_on_server) ftp.puttextfile(path_to_web_file) ftp.close
解决方法@H_502_8@
StringIO.new提供了一个像打开的文件一样的对象.通过使用StringIO对象而不是文件,创建一个像
puttextfile这样的方法很容易.
require 'net/ftp'
require 'stringio'
class Net::FTP
def puttextcontent(content,remotefile,&block)
f = StringIO.new(content)
begin
storlines("STOR " + remotefile,f,&block)
ensure
f.close
end
end
end
file_content = <<filecontent
<html>
<head><title>Hello!</title></head>
<body>Hello.</body>
</html>
filecontent
ftp = Net::FTP.new(domain)
ftp.passive = true
ftp.login(username,password)
ftp.chdir(path_on_server)
ftp.puttextcontent(file_content,path_to_web_file)
ftp.close
require 'net/ftp' require 'stringio' class Net::FTP def puttextcontent(content,remotefile,&block) f = StringIO.new(content) begin storlines("STOR " + remotefile,f,&block) ensure f.close end end end file_content = <<filecontent <html> <head><title>Hello!</title></head> <body>Hello.</body> </html> filecontent ftp = Net::FTP.new(domain) ftp.passive = true ftp.login(username,password) ftp.chdir(path_on_server) ftp.puttextcontent(file_content,path_to_web_file) ftp.close