简体   繁体   English

Net :: ftp getbinaryfile()保存到文件与保存到变量

[英]Net::ftp getbinaryfile() saving to file vs saving to variable

Using the following ftp_download method works, but if I change 使用以下ftp_download方法有效,但如果我改变了

ftp.getbinaryfile(file,localdir,1024)   #=> Saves the file to localdir

to

ftp.getbinaryfile(file)    #=> returns nil

I get nil returned. nil回来。 According to 根据

http://www.ruby-doc.org/stdlib-2.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-getbinaryfile http://www.ruby-doc.org/stdlib-2.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-getbinaryfile

inilf I set localfile to nil as above, the data should be retrieved and returned by the method. inilf我将localfile设置为nil ,如上所述,该方法应检索并返回数据。 What am I doing wrong? 我究竟做错了什么?

def ftp_download(domain,remotedir,filename_regex,user=nil,pwd=nil)
  ftp = Net::FTP::new(domain)
  if user && pwd
    ftp.login(user, pwd)
  end
  ftp.chdir(remotedir)
  fileList = ftp.nlst(filename_regex)

  fileList.each do |file|
    localdir=File.join(remotedir,file)
    localdir=localdir[1..-1] if localdir[0]="/"
    FileUtils.mkdir_p(File.dirname(localdir))
    ftp.getbinaryfile(file,localdir,1024)
  end
  ftp.close
end

If you look at the getbinaryfile method signature you will notice that the default value for the second parameter ( localfile ) is not nil but File.basename(remotefile) 如果查看getbinaryfile方法签名,您会注意到第二个参数( localfile )的默认值不是nil而是File.basename(remotefile)

getbinaryfile(remotefile, 
              localfile=File.basename(remotefile), 
              blocksize=DEFAULT_BLOCKSIZE)

If you want localfile to be nil you have to pass it explicitly: 如果您希望localfilenil ,则必须明确地传递它:

ftp.getbinaryfile(file, nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM