pankaj_it09 has asked for the wisdom of the Perl Monks concerning the following question:

The following is the code to search a file and display it.
The code is not working with pdf files when I try to open the file from a Windows machine. The code works when I open it from a Linux machine. The file is present in /tmp folder in Linux.
The error message which I get is -->
There was an error opening this document. The file is damaged and could not be repaired.
The error message comes only when I access the file from a Windows machine.
Please tell me why ?

sub searchFile { my $query = shift; my $filename = $query->param('filename'); my $conid = $query->param('conid'); my $datetime = $query->param('datetime'); my $oldfile=$filename; my ($date,$time) = split(/ /,$datetime); $filename="/tmp/$conid$date$time$filename"; $filename =~ s/[:-]//g; print $query->header(-type=> "application/octet-stream",-attachmen +t=>"$oldfile"); open(NEWFILE,"<$filename") or die "Couldn't open file '$filename': + $!"; binmode(NEWFILE); binmode STDOUT; print <NEWFILE>; close(NEWFILE); }

Replies are listed 'Best First'.
Re: PDF file opening from Linux but not opening from Windows
by almut (Canon) on Nov 24, 2006 at 14:06 UTC

    Not entirely sure I correctly understand what you're trying to do, but is there any reason to use the MIME-type "application/octet-stream"? In case you're trying to deliver PDF content, the MIME-type "application/pdf" would be more appropriate, I think... (also see this FAQ)

    On Windows, can you do "save link as..." or some such (i.e. save to disk), and then properly view the resulting file? If not, you might want to compare the contents of this file with the contents of your original /tmp/... file.

    There are a number of factors that could account for the differences in behaviour between Linux and Windows... So, some more info would be helpful if you need further comments -- e.g. which browser, PDF reader/plugin, MIME-type associations configured, etc.

      I got the solution.

      I did this -->
      print $query->header(-type=> "application/octet-stream",-attachment=>"$oldfile",-Content_Length => $size);

      I added the -Content_Length of the file . After that the pdf files started opening.
      Even if I use the MIME type "application/pdf" the problem remains the same.
      Moreover I have to open other types of files like excel etc. The excel files are opening nicely.
      Only problem is with PDF files .
      I tried saving the file also still the same error.
      On IE and mozilla I have tested it the same result.
      The PDF software is working nicely with the local files.
      Even if I use the MIME type "application/pdf" the problem remains the same.
      Moreover I have to open other types of files like excel etc. The excel files are opening nicely.
      Only problem is with PDF files .
      I tried saving the file also still the same error.
      On IE and mozilla I have tested it the same result.
      The PDF software is working nicely with the local files.
Re: PDF file opening from Linux but not opening from Windows
by fenLisesi (Priest) on Nov 24, 2006 at 12:53 UTC
    Hi, pankaj_it09 -- I am sure someone will soon solve your immediate problem, but I would like to remind you that when you asked a similar question before, a few people mentioned security problems with your code. I think you should turn on Taint mode. Cheers.
      Thanks fenLisesi.
      I got the solution by your suggestion.

      Thx Almut for ur help.