1

Why am I always getting corrupted image file when uploading to FTP server? .gif image doesn't get corrupted, only .jpeg/jpg and .png get corrupted.

sub png{
    my $ftp=Net::FTP->new($fhost)or die &ftpErr;
    $ftp->login($hostname, $hostpass);
    my $img=$ftp->put("$file");
    $ftp->get($img);
    $ftp->quit;
    our $image="$img";
    our $shot=$window->Photo(-format=>'png',-file=>"$image");
    $window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
sub jpeg{
    my $ftp=Net::FTP->new($fhost)or die &ftpErr;
    $ftp->login($hostname, $hostpass);
    my $img=$ftp->put("$file");
    $ftp->get($img);
    $ftp->quit;
    our $image="$img";
    our $shot=$window->Photo(-format=>'jpeg',-file=>"$image");
    $window->Label(-relief=>'ridge',-image=>$shot,-width=>50,-height=>50)->pack(-anchor=>'n');
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Muskovitz
  • 101
  • 7

1 Answers1

2

You are transferring the files in the default mode, which is ASCII. This mode translates line ends. To transfer binary files use binary mode:

  $ftp->binary;
  $ftp->put(...);
  $ftp->get(...);
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • 1
    Make sure the `$ftp->binary` is after login – CJ7 Feb 05 '18 at 01:00
  • @CJ7: Binary mode should be set before the upload/download. In fact, it can be set differently for each upload/download. – Steffen Ullrich Feb 05 '18 at 05:36
  • But it needs to be after the username and password step. – CJ7 Feb 05 '18 at 21:56
  • @CJ7: Net::FTP does not set the type by its own. It might be that some FTP servers will reset any type setting done before login. To be save it is the best to set the type immediately before beginning a transfer. – Steffen Ullrich Feb 06 '18 at 06:19
  • @CJ17 Thank you bro. I switch on binmode after object has been created. It was big trouble cause I need to do it after login immediately. – Orlov Const Sep 29 '19 at 12:23