Mason File Upload Progress Bar

Scott Dolan <[email protected]> Wed, 10 Nov 2010 15:53:24 +0000
Newsgroups gmane.comp.web.mason.devel
Message-ID <1ECA46F61C661046BD7A5B992386CDBE2404432A@MBX022-E1-NJ-5.exch022.domain.local>
--===============9024414266710215469==
Content-Language: en-US
Content-Type: multipart/alternative;
	boundary="_000_1ECA46F61C661046BD7A5B992386CDBE2404432AMBX022E1NJ5exch_"

--_000_1ECA46F61C661046BD7A5B992386CDBE2404432AMBX022E1NJ5exch_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

                I need to add a  file upload with a progress bar page to my=
 website that I developed with Mason.  I am already implemented a standard =
file upload with a progress bar using CGI (code below).  The script uses a =
callback hook function  my $q =3D new CGI (\&hook);  sub hook{ ...};      H=
owever, this code does not work within mason. It seems the mason component =
is only executed after the complete file has been received which prevents m=
e from sending responding back with the status of current upload process.
                Is there a way to modify how mason recieves the files so, I=
 can add a progress bar.  I am figuring, I am going to have to do some low =
end development to get this feature.  How do I go about developing such a f=
eature?  I think this would be valueable feature and necessary feature to h=
ave in the mason framework especially to make mason viable for new developm=
ent.  I am willing to do the work but, I need some direction.

Scott
732-300-9956


#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp "fatalsToBrowser";
use Data::Dumper;

# Make a file upload hook.
my $q =3D 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) =3D @_;

  # Get our sessid from the form submission.
  my ($sessid) =3D $ENV{QUERY_STRING};
      $sessid =3D~ 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 =3D $ENV{'CONTENT_LENGTH'};
  my $percent =3D 0;
  if ($length > 0)
  {
    # Don't divide by zero.
    $percent =3D sprintf("%.1f", (( $bytes_read / $length ) * 100) );
  }

  # Write this data to the session file.
  open (SES, "> ./files/$sessid.session");
  print SES "$bytes_read:$length:$percent";
  close (SES);

  open( DUM, ">> ./files/dumper.txt");
  print DUM "hook " . Dumper($ENV{REQUEST_METHOD});
  close( DUM );




--_000_1ECA46F61C661046BD7A5B992386CDBE2404432AMBX022E1NJ5exch_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40"><head><meta http-equiv=3DContent-Type content=
=3D"text/html; charset=3Dus-ascii"><meta name=3DGenerator content=3D"Micros=
oft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]--></head><body lang=3DEN-US link=3Dblue vli=
nk=3Dpurple><div class=3DWordSection1><p class=3DMsoNormal>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
I need to add a &nbsp;file upload with a progress bar page to my website th=
at I developed with Mason. &nbsp;I am already implemented a standard file u=
pload with a progress bar using CGI (code below).&nbsp; The script uses a c=
allback hook function&nbsp; my $q =3D new CGI (\&amp;hook);&nbsp; sub hook{=
&nbsp;&#8230;};&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;However, this code does not w=
ork within mason. It seems the mason component is only executed after the c=
omplete file has been received which prevents me from sending responding ba=
ck with the status of current upload process. &nbsp;<o:p></o:p></p><p class=
=3DMsoNormal>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp; Is there a way to modify how mason recieves th=
e files so, I can add a progress bar.&nbsp; I am figuring, I am going to ha=
ve to do some low end development to get this feature.&nbsp; How do I go ab=
out developing such a feature?&nbsp; I think this would be valueable featur=
e and necessary feature to have in the mason framework especially to make m=
ason viable for new development. &nbsp;I am willing to do the work but, I n=
eed some direction.<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p=
><p class=3DMsoNormal>Scott<o:p></o:p></p><p class=3DMsoNormal>732-300-9956=
<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNorm=
al><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>#!/usr/bin/perl<o:p></o:p></p>=
<p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>use strict;<=
o:p></o:p></p><p class=3DMsoNormal>use warnings;<o:p></o:p></p><p class=3DM=
soNormal>use CGI;<o:p></o:p></p><p class=3DMsoNormal>use CGI::Carp &quot;fa=
talsToBrowser&quot;;<o:p></o:p></p><p class=3DMsoNormal>use Data::Dumper;<o=
:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal=
># Make a file upload hook.<o:p></o:p></p><p class=3DMsoNormal>my $q =3D ne=
w CGI (\&amp;hook);<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p=
><p class=3DMsoNormal># This is the file upload hook, where we can update o=
ur session<o:p></o:p></p><p class=3DMsoNormal># file with the dirty details=
 of how the upload is going.<o:p></o:p></p><p class=3DMsoNormal>sub hook<o:=
p></o:p></p><p class=3DMsoNormal>{<o:p></o:p></p><p class=3DMsoNormal>&nbsp=
; my ($filename,$buffer,$bytes_read,$file) =3D @_;<o:p></o:p></p><p class=
=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&nbsp; # Get our ses=
sid from the form submission.<o:p></o:p></p><p class=3DMsoNormal>&nbsp; my =
($sessid) =3D $ENV{QUERY_STRING};<o:p></o:p></p><p class=3DMsoNormal>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; $sessid =3D~ s/[^A-F0-9]//g;<o:p></o:p></p><p clas=
s=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&nbsp; # Calculate =
the (rough estimation) of the file size. This isn't<o:p></o:p></p><p class=
=3DMsoNormal>&nbsp; # accurate because the CONTENT_LENGTH includes not only=
 the file's<o:p></o:p></p><p class=3DMsoNormal>&nbsp; # contents, but also =
the length of all the other form fields as well,<o:p></o:p></p><p class=3DM=
soNormal>&nbsp; # so it's bound to be at least a few bytes larger than the =
file size.<o:p></o:p></p><p class=3DMsoNormal>&nbsp; # This obviously doesn=
't work out well if you want progress bars on<o:p></o:p></p><p class=3DMsoN=
ormal>&nbsp; # a per-file basis, if uploading many files. This proof-of-con=
cept only<o:p></o:p></p><p class=3DMsoNormal>&nbsp; # supports a single fil=
e anyway.<o:p></o:p></p><p class=3DMsoNormal>&nbsp; my $length =3D $ENV{'CO=
NTENT_LENGTH'};<o:p></o:p></p><p class=3DMsoNormal>&nbsp; my $percent =3D 0=
;<o:p></o:p></p><p class=3DMsoNormal>&nbsp; if ($length &gt; 0)<o:p></o:p><=
/p><p class=3DMsoNormal>&nbsp; {<o:p></o:p></p><p class=3DMsoNormal>&nbsp;&=
nbsp;&nbsp; # Don't divide by zero.<o:p></o:p></p><p class=3DMsoNormal>&nbs=
p;&nbsp;&nbsp; $percent =3D sprintf(&quot;%.1f&quot;, (( $bytes_read / $len=
gth ) * 100) );<o:p></o:p></p><p class=3DMsoNormal>&nbsp; }<o:p></o:p></p><=
p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&nbsp; # Writ=
e this data to the session file.<o:p></o:p></p><p class=3DMsoNormal>&nbsp; =
open (SES, &quot;&gt; ./files/$sessid.session&quot;);<o:p></o:p></p><p clas=
s=3DMsoNormal>&nbsp; print SES &quot;$bytes_read:$length:$percent&quot;;<o:=
p></o:p></p><p class=3DMsoNormal>&nbsp; close (SES);<o:p></o:p></p><p class=
=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>&nbsp; open( DUM, &q=
uot;&gt;&gt; ./files/dumper.txt&quot;);<o:p></o:p></p><p class=3DMsoNormal>=
&nbsp; print DUM &quot;hook &quot; . Dumper($ENV{REQUEST_METHOD});<o:p></o:=
p></p><p class=3DMsoNormal>&nbsp; close( DUM );<o:p></o:p></p><p class=3DMs=
oNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p c=
lass=3DMsoNormal><o:p>&nbsp;</o:p></p></div></body></html>=

--_000_1ECA46F61C661046BD7A5B992386CDBE2404432AMBX022E1NJ5exch_--


--===============9024414266710215469==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
--===============9024414266710215469==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Mason-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mason-devel

--===============9024414266710215469==--