Rails upload file to ftp server

后端 未结 2 541
离开以前
离开以前 2020-12-28 09:01

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 b

相关标签:
2条回答
  • 2020-12-28 09:22

    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.

    0 讨论(0)
  • 2020-12-28 09:22

    Seems to me that ftp.putbinaryfile just wants the path and name of the file as the first parameter.

    0 讨论(0)
提交回复
热议问题