upload.cgi
Using perl Syntax Highlighting
- #!C:\wamp\bin\perl\bin\perl.exe
- use strict;
- use warnings;
- use CGI;
- use CGI::Carp "fatalsToBrowser";
- # Make a file upload hook.
- my $q = new CGI (\&hook);
- # This is the file upload hook, where we can update our session
- # file with the dirty details of how the upload is going.
- sub hook {
- my ($filename,$buffer,$bytes_read,$file) = @_;
- # Get our sessid from the form submission.
- my ($sessid) = $ENV{QUERY_STRING};
- $sessid =~ s/[^A-F0-9]//g;
- # Calculate the (rough estimation) of the file size. This isn't
- # accurate because the CONTENT_LENGTH includes not only the file's
- # contents, but also the length of all the other form fields as well,
- # so it's bound to be at least a few bytes larger than the file size.
- # This obviously doesn't work out well if you want progress bars on
- # a per-file basis, if uploading many files. This proof-of-concept only
- # supports a single file anyway.
- my $length = $ENV{'CONTENT_LENGTH'};
- my $percent = 0;
- if ($length > 0) { # Don't divide by zero.
- $percent = sprintf("%.1f",
- (( $bytes_read / $length ) * 100)
- );
- }
- # Write this data to the session file.
- open (SES, ">$sessid.session");
- print SES "$bytes_read:$length:$percent";
- close (SES);
- }
- # Now the meat of the CGI script.
- print "Content-Type: text/html\n\n";
- my $action = $q->param("do") || "unknown";
- if ($action eq "upload") {
- # They are first submitting the file. This code doesn't really run much
- # until AFTER the file is completely uploaded.
- my $filename = $q->param("incoming");
- my $handle = $q->upload("incoming");
- my $sessid = $q->param("sessid");
- $sessid =~ s/[^A-F0-9]//g;
- $filename =~ s/(?:\\|\/)([^\\\/]+)$/$1/g;
- # Copy the file to its final location.
- open (FILE, ">./files/$filename") or die "Can't create file: $!";
- binmode(FILE); #Para no tener problemas en Windows
- my $buffer;
- while (read($handle,$buffer,2048)) {
- print FILE $buffer;
- }
- close (FILE);
- # Delete the session file.
- unlink("./$sessid.session");
- # Done.
- print "Thank you for your file. <a href=\"files/$filename\">Here it is again.</a>";
- }
- elsif ($action eq "ping") {
- # Checking up on the status of the upload.
- my $sessid = $q->param("sessid");
- $sessid =~ s/[^A-F0-9]//g;
- # Exists?
- if (-f "./$sessid.session") {
- # Read it.
- open (READ, "./$sessid.session");
- my $data = <READ>;
- close (READ);
- print $data;
- }
- else {
- print "0:0:0:error session $sessid doesn't exist";
- }
- }
- else {
- print "0:0:0:error invalid action $action";
- }
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4
upload.html
Using html4strict Syntax Highlighting
- <!DOCTYPE html>
- <html>
- <head>
- <title>Upload Test</title>
- <style type="text/css">
- body {
- background-color: #FFFFFF;
- font-family: Verdana,Arial,sans-serif;
- font-size: small;
- color: #000000
- }
- #trough {
- border: 1px solid #000000;
- height: 16px;
- display: block;
- background-color: #DDDDDD
- }
- #bar {
- background-color: #0000FF;
- background-image: url("blue-clearlooks.png");
- border-right: 1px solid #000000;
- height: 16px
- }
- </style>
- </head>
- <body>
- <h1>File Upload Test</h1>
- <div id="progress-div" style="display: none; width: 400px; margin: auto">
- <fieldset>
- <legend>Upload Progress</legend>
- <div id="trough">
- <div id="bar" style="width: 0%"></div>
- </div>
- Received <span id="received">0</span>/<span id="total">0</span> (<span id="percent">0</span>%)
- </fieldset>
- </div>
- <div id="upload-form" style="display: block; width: 600px; margin: auto">
- <fieldset>
- <legend>Upload a File</legend>
- <form name="upload" method="post" action="upload.cgi" enctype="multipart/form-data" onSubmit="return startUpload()" id="theform">
- <input type="hidden" name="do" value="upload">
- <table border="0" cellspacing="0" cellpadding="2">
- <tr>
- <td align="left" valign="middle">
- Session ID<span style="color: #FF0000">*</span>:
- </td>
- <td align="left" valign="middle">
- <input type="text" size="40" name="sessid" id="sessid" readonly="readonly">
- </td>
- </tr>
- <tr>
- <td align="left" valign="middle">
- File:
- </td>
- <td align="left" valign="middle">
- <input type="file" name="incoming" size="40">
- </td>
- </tr>
- </table><p>
- <input type="submit" value="Upload It!"><p>
- <small>
- <span style="color: #FF0000">*</span> Randomly generated by JavaScript. In practice this would be
- randomly generated by server-side script and "hard-coded" into the HTML you see on this page.
- </small>
- </fieldset>
- </div>
- <div id="debug"></div>
- <script type="text/javascript">
- // a jquery-like function, a shortcut to document.getElementById
- function $(o) {
- return document.getElementById(o);
- }
- // called on page load to make up a session ID (in real life the session ID
- // would be made up via server-side script and "hard-coded" in the HTML received
- // by the server, thus it wouldn't require javascript at all)
- function init() {
- // Make up a session ID.
- var hex = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
- "A", "B", "C", "D", "E", "F" ];
- var ses = "";
- for (var i = 0; i < 8; i++) {
- var rnd = Math.floor(Math.random()*16);
- ses += hex[rnd];
- }
- $("sessid").value = ses;
- // we set the form action to send the sessid in the query string, too.
- // this way it's available inside the CGI hook function in a very easy
- // way. In real life this would probably be done better.
- $("theform").action += "?" + ses;
- }
- window.onload = init;
- // This function is called when submitting the form.
- function startUpload() {
- // Hide the form.
- $("upload-form").style.display = "none";
- // Show the progress div.
- $("progress-div").style.display = "block";
- // Begin making ajax requests.
- setTimeout("ping()", 1000);
- // Allow the form to continue submitting.
- return true;
- }
- // Make an ajax request to check up on the status of the upload
- function ping() {
- var ajax = new XMLHttpRequest();
- ajax.onreadystatechange = function () {
- if (ajax.readyState == 4) {
- parse(ajax.responseText);
- }
- };
- ajax.open("GET", "upload.cgi?do=ping&sessid=" + $("sessid").value + "&rand=" + Math.floor(Math.random()*99999), true);
- ajax.send(null);
- }
- // React to the returned value of our ping test
- function parse(txt) {
- $("debug").innerHTML = "received from server: " + txt;
- var parts = txt.split(":");
- if (parts.length == 3) {
- $("received").innerHTML = parts[0];
- $("total").innerHTML = parts[1];
- $("percent").innerHTML = parts[2];
- $("bar").style.width = parts[2] + "%";
- }
- // Ping again!
- setTimeout("ping()", 1000);
- }
- </script>
- </body>
- </html>
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4
Bueno ahí esta el ejemplo, espero puedan ayudarme a que funcione en chrome.