2017-02-25 111 views
1

我有一些ruby代碼,我正用它從FTP服務器下載csv文件。從ftp下載csv文件與ruby sinatra

但是,現在,但它不工作,並沒有顯示任何錯誤消息。

require 'net/ftp' 
require 'net/ftp' 
require 'fileutils' 

get '/romil' do 
localfile = 'C:\\Users\\dell\\Desktop\\test1.csv' 
ftp = Net::FTP.new(CONTENT_SERVER_DOMAIN_NAME) 
ftp.login CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD 
ftp.passive = true 

files = ftp.chdir('abhi/') 
files = ftp.list 
puts "list out of directory:" 
puts files 
ftp.gettextfile('test.csv', localfile, 1024) 

ftp.close 
end 
+0

輕微格式化的調整和拼寫 –

回答

0

好的人。我得到了答案, 這是有點棘手, 這裏是工作代碼:

get '/romil' do 
    ftp = Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME) do |ftp| 
    ftp.login CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD 
    ftp.passive = true 
    files = ftp.chdir('abhi/') 
    files = ftp.list 
    puts "list out of directory:" 
    puts files 
    ftp.gettextfile('test7.csv') 
    filename = 'test7.csv' 
    str = '' 
    CSV.foreach(filename, headers: true) do |row| 
    status 200 
    headers \ 
    "Content-Type" => "text\\plain" 
    str = str + row[0] + ' ' + row[1]+ "\n" 
    end 
    body str 
end 
end