• Publicidad

Barra de progreso

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Re: Barra de progreso

Notapor tinchosan » 2011-09-03 09:44 @447

Gracias explorer, buscando por la web, encontré otra solución. El problema es que funciona correctamente en Mozilla y no en Chrome, sabrás de algún fix para ello? adjunto el ejemplo original que encontre.

upload.cgi
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!C:\wamp\bin\perl\bin\perl.exe
  2. use strict;
  3. use warnings;
  4. use CGI;
  5. use CGI::Carp "fatalsToBrowser";
  6.  
  7. # Make a file upload hook.
  8. my $q = new CGI (\&hook);
  9.  
  10. # This is the file upload hook, where we can update our session
  11. # file with the dirty details of how the upload is going.
  12. sub hook {
  13.         my ($filename,$buffer,$bytes_read,$file) = @_;
  14.  
  15.         # Get our sessid from the form submission.
  16.         my ($sessid) = $ENV{QUERY_STRING};
  17.         $sessid =~ s/[^A-F0-9]//g;
  18.  
  19.         # Calculate the (rough estimation) of the file size. This isn't
  20.         # accurate because the CONTENT_LENGTH includes not only the file's
  21.         # contents, but also the length of all the other form fields as well,
  22.         # so it's bound to be at least a few bytes larger than the file size.
  23.         # This obviously doesn't work out well if you want progress bars on
  24.         # a per-file basis, if uploading many files. This proof-of-concept only
  25.         # supports a single file anyway.
  26.         my $length = $ENV{'CONTENT_LENGTH'};
  27.         my $percent = 0;
  28.         if ($length > 0) { # Don't divide by zero.
  29.                 $percent = sprintf("%.1f",
  30.                         (( $bytes_read / $length ) * 100)
  31.                 );
  32.         }
  33.  
  34.         # Write this data to the session file.
  35.         open (SES, ">$sessid.session");
  36.         print SES "$bytes_read:$length:$percent";
  37.         close (SES);
  38. }
  39.  
  40. # Now the meat of the CGI script.
  41. print "Content-Type: text/html\n\n";
  42.  
  43. my $action = $q->param("do") || "unknown";
  44. if ($action eq "upload") {
  45.         # They are first submitting the file. This code doesn't really run much
  46.         # until AFTER the file is completely uploaded.
  47.         my $filename = $q->param("incoming");
  48.         my $handle   = $q->upload("incoming");
  49.         my $sessid   = $q->param("sessid");
  50.         $sessid     =~ s/[^A-F0-9]//g;
  51.         $filename =~ s/(?:\\|\/)([^\\\/]+)$/$1/g;
  52.  
  53.         # Copy the file to its final location.
  54.         open (FILE, ">./files/$filename") or die "Can't create file: $!";
  55.         binmode(FILE); #Para no tener problemas en Windows
  56.         my $buffer;
  57.         while (read($handle,$buffer,2048)) {
  58.                 print FILE $buffer;
  59.         }
  60.         close (FILE);
  61.  
  62.         # Delete the session file.
  63.         unlink("./$sessid.session");
  64.  
  65.         # Done.
  66.         print "Thank you for your file. <a href=\"files/$filename\">Here it is again.</a>";
  67. }
  68. elsif ($action eq "ping") {
  69.         # Checking up on the status of the upload.
  70.         my $sessid = $q->param("sessid");
  71.         $sessid =~ s/[^A-F0-9]//g;
  72.  
  73.         # Exists?
  74.         if (-f "./$sessid.session") {
  75.                 # Read it.
  76.                 open (READ, "./$sessid.session");
  77.                 my $data = <READ>;
  78.                 close (READ);
  79.                 print $data;
  80.         }
  81.         else {
  82.                 print "0:0:0:error session $sessid doesn't exist";
  83.         }
  84. }
  85. else {
  86.         print "0:0:0:error invalid action $action";
  87. }
  88.  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


upload.html
Sintáxis: [ Descargar ] [ Ocultar ]
Using html4strict Syntax Highlighting
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Upload Test</title>
  5. <style type="text/css">
  6. body {
  7.  background-color: #FFFFFF;
  8.  font-family: Verdana,Arial,sans-serif;
  9.  font-size: small;
  10.  color: #000000
  11. }
  12. #trough {
  13.  border: 1px solid #000000;
  14.  height: 16px;
  15.  display: block;
  16.  background-color: #DDDDDD
  17. }
  18. #bar {
  19.  background-color: #0000FF;
  20.  background-image: url("blue-clearlooks.png");
  21.  border-right: 1px solid #000000;
  22.  height: 16px
  23. }
  24. </style>
  25. </head>
  26. <body>
  27.  
  28. <h1>File Upload Test</h1>
  29.  
  30. <div id="progress-div" style="display: none; width: 400px; margin: auto">
  31.         <fieldset>
  32.                 <legend>Upload Progress</legend>
  33.                 <div id="trough">
  34.                         <div id="bar" style="width: 0%"></div>
  35.                 </div>
  36.                 Received <span id="received">0</span>/<span id="total">0</span> (<span id="percent">0</span>%)
  37.         </fieldset>
  38. </div>
  39.  
  40. <div id="upload-form" style="display: block; width: 600px; margin: auto">
  41.         <fieldset>
  42.                 <legend>Upload a File</legend>
  43.                 <form name="upload" method="post" action="upload.cgi" enctype="multipart/form-data" onSubmit="return startUpload()" id="theform">
  44.                 <input type="hidden" name="do" value="upload">
  45.  
  46.                 <table border="0" cellspacing="0" cellpadding="2">
  47.                         <tr>
  48.                                 <td align="left" valign="middle">
  49.                                         Session ID<span style="color: #FF0000">*</span>:
  50.                                 </td>
  51.                                 <td align="left" valign="middle">
  52.                                         <input type="text" size="40" name="sessid" id="sessid" readonly="readonly">
  53.                                 </td>
  54.                         </tr>
  55.                         <tr>
  56.                                 <td align="left" valign="middle">
  57.                                         File:
  58.                                 </td>
  59.                                 <td align="left" valign="middle">
  60.                                         <input type="file" name="incoming" size="40">
  61.                                 </td>
  62.                         </tr>
  63.                 </table><p>
  64.  
  65.                 <input type="submit" value="Upload It!"><p>
  66.  
  67.                 <small>
  68.                 <span style="color: #FF0000">*</span> Randomly generated by JavaScript. In practice this would be
  69.                 randomly generated by server-side script and "hard-coded" into the HTML you see on this page.
  70.                 </small>
  71.         </fieldset>
  72. </div>
  73.  
  74. <div id="debug"></div>
  75.  
  76. <script type="text/javascript">
  77. // a jquery-like function, a shortcut to document.getElementById
  78. function $(o) {
  79.         return document.getElementById(o);
  80. }
  81.  
  82. // called on page load to make up a session ID (in real life the session ID
  83. // would be made up via server-side script and "hard-coded" in the HTML received
  84. // by the server, thus it wouldn't require javascript at all)
  85. function init() {
  86.         // Make up a session ID.
  87.         var hex = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  88.         "A", "B", "C", "D", "E", "F" ];
  89.         var ses = "";
  90.  
  91.         for (var i = 0; i < 8; i++) {
  92.                 var rnd = Math.floor(Math.random()*16);
  93.                 ses += hex[rnd];
  94.         }
  95.  
  96.         $("sessid").value = ses;
  97.  
  98.         // we set the form action to send the sessid in the query string, too.
  99.         // this way it's available inside the CGI hook function in a very easy
  100.         // way. In real life this would probably be done better.
  101.         $("theform").action += "?" + ses;
  102. }
  103. window.onload = init;
  104. // This function is called when submitting the form.
  105. function startUpload() {
  106.         // Hide the form.
  107.         $("upload-form").style.display = "none";
  108.         // Show the progress div.
  109.         $("progress-div").style.display = "block";
  110.         // Begin making ajax requests.
  111.         setTimeout("ping()", 1000);
  112.         // Allow the form to continue submitting.
  113.         return true;
  114. }
  115. // Make an ajax request to check up on the status of the upload
  116. function ping() {
  117.         var ajax = new XMLHttpRequest();
  118.         ajax.onreadystatechange = function () {
  119.                 if (ajax.readyState == 4) {
  120.                         parse(ajax.responseText);
  121.                 }
  122.         };
  123.         ajax.open("GET", "upload.cgi?do=ping&sessid=" + $("sessid").value + "&rand=" + Math.floor(Math.random()*99999), true);
  124.         ajax.send(null);
  125. }
  126. // React to the returned value of our ping test
  127. function parse(txt) {
  128.         $("debug").innerHTML = "received from server: " + txt;
  129.         var parts = txt.split(":");
  130.         if (parts.length == 3) {
  131.                 $("received").innerHTML = parts[0];
  132.                 $("total").innerHTML = parts[1];
  133.                 $("percent").innerHTML = parts[2];
  134.                 $("bar").style.width = parts[2] + "%";
  135.         }
  136.         // Ping again!
  137.         setTimeout("ping()", 1000);
  138. }
  139. </script>
  140.  
  141. </body>
  142. </html>
  143.  
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4


Bueno ahí esta el ejemplo, espero puedan ayudarme a que funcione en chrome.
tinchosan
Perlero nuevo
Perlero nuevo
 
Mensajes: 6
Registrado: 2011-09-01 11:28 @519

Publicidad

Anterior

Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: Google [Bot] y 35 invitados

cron