简体   繁体   中英

Concatenate bytes - Writing file to FTP folder as chunks

I'm writing an Rails app which could upload files to storage. Big files are splitted into chunks from client (with JS) and upload parts to server.

As in development, I could simply open existed file and write following bytes into that.

(I'm using CarrierWave gem)

File.open(@up_file.link.path, "ab") do |f|
   f.write(up_file_params[:link].read)
end
# This code worked when I upload to '/public' folder in development

However, now I want to use a FTP server to storage files. But I can't Concatenate new bytes with existed bytes.

def get_ftp_connection    #  create a new FTP connection
    ftp = Net::FTP.new        
    ftp.connect(ENV['ftp_host'], ENV['ftp_port'])

    begin
        ftp.passive = ENV['ftp_passive']
        ftp.login(ENV['ftp_user'], ENV['ftp_passwd'])

        yield ftp
    ensure
        ftp.quit
    end
end

.....
def create
    .....
    get_ftp_connection @up_file do |ftp|
       full_path = ::File.dirname "#{ENV['ftp_folder']}/#{@up_file.link.path}"
       base_name = File.basename(@up_file.link.to_s)
       ftp.chdir(full_path)                
       ftp.putbinaryfile(up_file_params[:link].read, base_name)
    end
end

I got ArgumentError (string contains null byte): at putbinaryfile... , any help :(

As mentioned in the comments you could completely download the file first and then upload to the ftp server. If that's not an option for whatever reason, you could append to the remote file as it's being uploaded:

ftp.storbinary("APPE #{base_name}", up_file_params[:link], Net::FTP::DEFAULT_BLOCKSIZE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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