Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue to open a text file 1

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I call a script from a Web page to download a file, this Perl script and these files to download are located in Linux server. I can download a text file on Windows station but when I opened it, it contained one line instead many lines, the end paragraph is remplaced by a small square (end line and not end of paragraph). If I try to open it from the Linux server via a Linux command as "cat test.txt", the file will be OK. I think that it is related to my Perl script (see below).

Can someone help me to fix this issue?


Thanks
======================

open(BFILE, '<', "$file") or return(0);
print $q->header(-type => 'application/x-download',
-attachment => "$file",
'Content-length' => -s "$file"
);
binmode BFILE;
print while <BFILE>;
close (BFILE);
 
For starters, change your first line to
Code:
open(BFILE, $file) or return 0 ;
 
Hi PinkeyNBrain,

I tried your suggestion but it does not work. Have you any other suggestion?

Thanks
 
Yes, I would suggest commenting out the binmode lines and see what happens. Another way to go about it if you feel you want to try a different approach is something like:

Code:
open(BFILE, "<$file") || return;  
@fileData = <BFILE>;  
close (BFILE);  
print "Content-Type:application/x-download\n";  
print "Content-Disposition:attachment; filename=$file\n\n";
print @fileData;

Scott Prelewicz
Web Developer
COMAND Solutions
 
Hi ScottPrelewicz,

I tried your sugestion and I got this error message:
Internal Server Error

Thanks
 
Hi Audiopro,

The line feeds will not be ingnored when I download a file in binmode.
 
Something is going to have to translate the Linux/Unix line endings to windows line endings. Is it only text files that you're downloading from the server?

 
Can you get an error message for me that caused your 500? I've used code like this before with good results.

Scott Prelewicz
Web Developer
COMAND Solutions
 
As for the line endings
Code:
# define this somewhere
$crlf = chr(13) . chr(10) ;
# Then use as
$data_that_needs_line_endings_removed =~ s/[$crlf]{1,2}$//g;
print $data_that_needs_line_endings_removed . "\n" ;
The above will strip off line endings from both *nix and win platforms. The "\n" will add back on the appropriate line ending for the platform the code is running on. The {1,2} may be overkill depending on your source data.

As for the Internal Server Error, don't have good advice on that one. Scott's code suggestion should have worked.
 
I can download a text file on Windows station but when I opened it

What did you use to open the file? Have you tried a differnt text editor to open the file to make sure the problem is not something specific to the application that prints the file as one line?

Normally, Windows can handle \n for the line endings. Its native line endings is \r\n but I open files with only \n line endings in Windows and they appear fine. I suspect the line endings are something unusual and maybe not \n.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

If I use WordPad or Microsoft Word to open this text file, the file will be opened well with many lines. But if I use Notepad, the file will be opened in one line.
 
Yeah, Notepad is one Windows program I know of that treats a single \n character as an unprintable symbol (a block).

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Hi,

I found a solution for my problem:

while ( <BFILE> ) {
$l=$_; $l =~ s/\n/\r\n/g; print $l;
}
close (BFILE);


Thanks for your solution......
 
I usually don't get into the heavy duty minimal code stuff, but would the following work as well?
Code:
 print map {s/\n/\r\n/} <BFILE> ;
 
Will this work?

my $ifh = 'linuxFile';
my $ofh = 'newfile.txt';
open(IFH,$ifh);
open(OFH,">$ofh");
while ( <IFH> ) {
s/\W+$/\n/;
print OFH;
}
close IFH;
close OFH;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top