[SOLVED] FTP Upload any file


#1

Hi,

I’m fairly new around rust. So please be patient with me :slight_smile:

I tried to upload file (image/zip) to FTP, it’s ok with example provided by https://github.com/mattnenterprise/rust-ftp (ftp 2.0.0 cargo) and i stuck.

What should I do? I’ve tried with opening file and read as VEC and String (based on http://stackoverflow.com/questions/31192956/whats-the-de-facto-way-of-reading-and-writing-files-now) And failed.

What is the proper way to upload specified file by path to ftp? Couldn’t find a solution around.

Thanks for tips and help :slight_smile:

edit:
> let mut file = File::open("/home/emjot/Pictures/test2.png").unwrap();

// what should be here?
let _ = ftp_stream.put("greeting.png", &mut _AND_HERE_);

#2

Hi LoU and welcome to Rust!

So

ftp_stream.put()

expects anything that implements the Read trait:

http://mattnenterprise.github.io/rust-ftp/ftp/struct.FtpStream.html#method.put

https://doc.rust-lang.org/nightly/std/io/trait.Read.html

And File implements the Read trait:

https://doc.rust-lang.org/std/fs/struct.File.html

So this should work (untested):

let mut file = File::open("/home/emjot/Pictures/test2.png").unwrap();
ftp_stream.transfer_type(FileType::Image)
let _ = ftp_stream.put("greeting.png", &mut file);

I’ve added the transfer type to ensure that binary transfer is used instead of text mode.

Hope this helps!


#3

Hi Will,

Thanks for reply. I’ve tried using transfer_type before and couldn’t figure out how to do this properly.

Every time it threw error while compiling:
with: use use std::fs::FileType;

no associated item named Image found for type std::fs::FileType in the current scope

without:use use std::fs::FileType;

failed to resolve. Use of undeclared type or module FileType [E0433]

Also tried Binary instead of Image, yet same error occurs.


#4

Ah sorry, the enum FileType is from the ftp crate and thus must be imported like this:

use ftp::types::FileType;

#5

Thanks @willi_kappler it worked.

Once again thanks for help and warm welcome, and patience :slight_smile:

Tested with binary with :

use ftp::types::FileType;

let mut file = File::open("/home/emjot/Pictures/test2.png").unwrap();
     ftp_stream.transfer_type(FileType::Binary);
    let _ = ftp_stream.put("greeting.png", &mut file);