Results 1 to 3 of 3

Thread: Downloading file in perl

  1. #1
    Registered
    Join Date
    Aug 2009
    Location
    texas
    Posts
    26
    Rep Power
    0

    Downloading file in perl

    I want to download an ascii file using PERL. I successfully download, the problem is the file downloaded doesn't keep the carriage return (like it downloaded binary instead of ascii).

    This is the code:

    open (DLFILE, "<$ID") || Error('open', 'file');
    @fileholder = <DLFILE>;
    close (DLFILE) || Error('close', 'file');

    # log the download - need to create dl.log file and chmod to 777

    open (LOG, ">>/home/e/f/0/34020/34020/public_html/dl.log") || Error('open', 'file');
    print LOG "$ID\n";
    close (LOG);

    # coax the browers into displaying the "save as.." dialog box by faking the file type in the headers that is sent

    print "Content-Type:application/x-download\n";
    print "Content-Disposition:attachment;filename=$ID\n\n";
    print @fileholder

    Thanks for any assistance!

  2. #2
    Rick
    Join Date
    May 2002
    Location
    Minneapolis, MN
    Posts
    1,753
    Rep Power
    19
    This issue usually boils down to a difference in line termination characters among the various operating systems involved - Windows, Mac, and Unix. The solution is to scan the data to determine the format and then change the line termination characters as needed.

    Since your script loads the data into an array, I'd suggest sending the data one line (element) at a time and do the translation as each line is sent. One way would be to replace all \r's with \n's and then replace any \n\n sequences with a single \n. Good luck!
    Rick Trethewey

  3. #3
    Registered
    Join Date
    Sep 2012
    Location
    USA
    Posts
    1
    Rep Power
    0
    I solved this.

    1.Detect the file type using -T if (-T $File_name) { #file is ascii text} else {#file is binary}

    2. If the file is ascii, send one line at a time and append \r\n onto that line
    foreach $line (@arrayoffile) { print "$line\r\n";}

    The file then downloaded correctly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •