Unable to save file in Browser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkirshteyn
    New Member
    • Aug 2007
    • 2

    Unable to save file in Browser

    Hi, I acquired a script, which is supposed to allow the user to save the generated content as a file. For some reason, the "Save As" option does not get displayed, and the generated content only gets displayed within a browser windows. Is it possible that there is some setting within the IE browser that is causing this to happen? Please help...

    This is the section of the code that is designed to allow saving the content as a file.

    [code=perl]
    if ($needcsv) {
    print "Content-type: application/octet-stream\n\n"
    } else {
    print "Content-type: text/html; charset=utf-8\n\n"
    [/code]
    Last edited by numberwhun; Aug 31 '07, 06:32 PM. Reason: Add code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Actually, all that section of code does is to set the Content-type for the document. From what I see, there is nothing being saved. Can you post more code, or all of the code, so we can help troubleshoot? Please let us know if there are any errors.

    Also, I have edited your post to add code tags. Please be sure and add them to any future code postings.

    Regards,

    Jeff

    Comment

    • mkirshteyn
      New Member
      • Aug 2007
      • 2

      #3
      Thanks for replying Jeff,

      I have slim to none experience with Perl. So any help is appreciated. Let me know if anything in this section seems to indicate the issue.

      Thanks,
      Mark.

      [CODE=perl]
      # Let's see what the browser wants: the browser is requesting a page from the Web server
      my $request = new CGI;

      #The import_names() method of the request object gets the parameters chosen by the user
      # to avoid confusion with perl variables those parameters are in a different namespace: R
      # the new namespace is put between quotes
      $request->import_names(' R');
      my $MAX_LINES = 50 ;
      my $inputlines ;
      my $function = $R::function; # this variable comes from the http request
      my $context = $R::context; # this variable comes from the http request
      my $csv_choice = $R::outcsv ;
      my $needcsv = 0 ;
      if ("$csv_choic e" eq "Yes") { $needcsv = 1 };

      # Maybe we have a .csv
      my $upload = $request->upload('file') ;
      my $tupload = $request->tmpFileName($u pload);
      if ( -s "$tupload") {
      open(TFH,"$tupl oad");
      while (<TFH>) {
      s/\n$//;
      s/\r$//;
      $inputlines[++$#inputlines] = $_ ;
      }
      close(TFH);
      } else { # ... or maybe we don't
      @inputlines = split(/\r?\n/,$R::users) if ("$function" eq "submit");
      }

      # Start sending back some data
      # Headers
      if ($needcsv) {
      print "Content-type: application/octet-stream\n\n"
      } else {
      print "Content-type: text/html; charset=utf-8\n\n"
      }

      # Start of HTML content
      unless ($needcsv) {
      print <<EOF;
      <HTML>

      <HEAD>
      <TITLE>Cognos LDAP - Access &amp; Administration Tool </TITLE>
      <LINK REL="stylesheet " HREF="$styleshe et" TYPE="text/css" />
      </HEAD>

      <BODY>
      <SCRIPT LANGUAGE="JavaS cript">

      function radio_button_ch ecker()
      {
      var radio_choice = false;

      for (counter = 0; counter < gaga.server.len gth; counter++)
      {
      if (gaga.server[counter].checked)
      radio_choice = true;
      }

      if (!radio_choice)
      {
      alert("Please select a environment.")
      return (false);
      }
      return (true);
      }

      </SCRIPT>
      EOF

      # Print banner on top with links
      foreach $key (sort { $contexts{$a}{i ndex} <=> $contexts{$b}{i ndex} } keys %contexts) {
      print "<A HREF=\"?functio n=form&context= $key\">[$contexts{$key} {stitle}]</A>\n";
      }

      # Print title of the page
      print "\n<br><br><hr> ";
      print "\n<TABLE align='center'> <TR><TD>";
      print "\n<H3 align='center'> COGNOS LDAP - Access &amp; Administration Tool</H3>\n";
      print "\n</TD></TR></TABLE>";

      [/CODE]

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        very unusual to see the import_names() function actually used in a script.

        Code:
        $request->import_names('R');
        my $MAX_LINES = 50 ;
        my $inputlines ;
        my $function = $R::function;    # this variable comes from the http request
        my $context  = $R::context;     # this variable comes from the http request
        my $csv_choice = $R::outcsv ;
        You probably should just be using the params() function to get these variables into your script. I see no advantage to using the import_names() unction for this smple script. So the above lines, unless there is a reason to use import_names(), would be:


        Code:
        my $MAX_LINES = 50 ;
        my $inputlines ;
        my $function = param('function');    # this variable comes from the http request
        my $context  = param('context');     # this variable comes from the http request
        my $csv_choice = param('outcsv');
        The problem with the "save as" box appears to be associated with these lines:

        my $needcsv = 0 ;
        if ("$csv_choic e" eq "Yes") { $needcsv = 1 };


        $needscsv must still be 0 (zero) later in the script which means the regular html document header is printed instead of the one that can open the "save as" box. There are also other worrysome things in the code, quoting single scalars is a very bad habit and is done repeatedly:

        if ("$csv_choic e" eq "Yes") { $needcsv = 1 };

        # Maybe we have a .csv
        my $upload = $request->upload('file') ;
        my $tupload = $request->tmpFileName($u pload);
        if ( -s "$tupload") {
        open(TFH,"$tupload");
        while (<TFH>) {
        s/\n$//;
        s/\r$//;
        $inputlines[++$#inputlines] = $_ ;
        }
        close(TFH);
        } else { # ... or maybe we don't
        @inputlines = split(/\r?\n/,$R::users) if ("$function" eq "submit");
        }

        that indicates the author is not well versed in perl coding. The quotes can hide syntax errors that make debugging a script near impossible.

        Later the code sorts a multidimensiona l hash to print a banner:

        # Print banner on top with links
        foreach $key (sort { $contexts{$a}{i ndex} <=> $contexts{$b}{i ndex} } keys %contexts) {
        print "<A HREF=\"?functio n=form&context= $key\">[$contexts{$key} {stitle}]</A>\n";
        }

        but there is no hash %context in the script to sort. Maybe that is a section of the script you did not post.

        Comment

        Working...