Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 9d8427f5d1078778f9470dbf7a99306fddbaf674 @drogus committed Jun 27, 2008
Showing with 182 additions and 0 deletions.
  1. +11 −0 .project
  2. +25 −0 example/.tmp_index.html~
  3. +50 −0 example/index.html
  4. 0 example/upload.html
  5. +64 −0 jquery.uploadProgress.js
  6. +32 −0 lib/jquery.js
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>jquery-upload-progress</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ </natures>
+</projectDescription>
@@ -0,0 +1,25 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <title>ajaxFileUpload</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <script src="../lib/jquery.js"></script>
+ <script src="../jquery.ajaxFileUpload.js"></script>
+ <script type="text/javascript">
+ $(function() {
+ $('form').ajaxFileUpload({uploadFinished: function() {
+ alert('skonczone');
+ }});
+ });
+ </script>
+ </head>
+
+ <body>
+ <form method="post" enctype="multipart/form-data" action="upload.html">
+ <input type="file" />
+ <input type="submit" />
+ </form>
+ </body>
+</html>
+
@@ -0,0 +1,50 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <title>ajaxFileUpload</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <script src="../lib/jquery.js"></script>
+ <script src="../jquery.uploadProgress.js"></script>
+ <script type="text/javascript">
+ $(function() {
+ $('form').uploadProgress({
+ start:function(){},
+ uploading: function(upload) {$('#percents').html(upload.percents+'%');},
+ interval: 2000
+ });
+ });
+ </script>
+ <style type="text/css">
+ .bar {
+ width: 300px;
+ }
+
+ #progress {
+ background: #eee;
+ border: 1px solid #222;
+ margin-top: 20px;
+ }
+ #progressbar {
+ width: 0px;
+ height: 24px;
+ background: #333;
+ }
+ </style>
+ </head>
+
+ <body>
+ <form id="upload" enctype="multipart/form-data" action="index.html" method="post">
+ <input name="file" type="file"/>
+ <input type="submit" value="Upload"/>
+ </form>
+
+ <div id="uploading">
+ <div id="progress" class="bar">
+ <div id="progressbar">&nbsp;</div>
+ </div>
+ </div>
+ <div id="percents"></div>
+ </body>
+</html>
+
No changes.
@@ -0,0 +1,64 @@
+/*
+ * jquery.uploadProgress
+ *
+ * Copyright (c) 2008 Piotr Sarnacki (drogomir.com)
+ *
+ * Licensed under the MIT license:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ */
+
+(function($) {
+ $.fn.uploadProgress = function(options) {
+ return this.each(function(){
+ $(this).bind('submit', function() {
+ var uuid = "";
+ for (i = 0; i < 32; i++) { uuid += Math.floor(Math.random() * 16).toString(16); }
+
+ options = $.extend({
+ interval: 2000,
+ progressBar: "#progressbar",
+ progressUrl: "/progress",
+ uuid: uuid,
+ start: function() {},
+ uploading: function() {},
+ complete: function() {},
+ timer: ""
+ }, options);
+ /* start callback */
+ options.start();
+
+ /* patch the form-action tag to include the progress-id */
+ $(this).attr("action", jQuery(this).attr("action") + "?X-Progress-ID=" + uuid);
+
+ options.timer = window.setInterval(function() { $.uploadProgress(this, options) }, options.interval);
+ });
+ });
+ };
+
+jQuery.uploadProgress = function(e, options) {
+ jQuery.ajax({
+ type: "GET",
+ url: options.progressUrl,
+ dataType: "json",
+ beforeSend: function(xhr) {
+ xhr.setRequestHeader("X-Progress-ID", options.uuid);
+ },
+ success: function(upload) {
+ if (upload.state == 'uploading') {
+ upload = $.extend({
+ percents: Math.floor((upload.received / upload.size)*1000)/10
+ }, upload);
+ $(options.progressBar).width(Math.floor(upload.percents) + '%');
+ options.uploading(upload);
+ }
+ /* we are done, stop the interval */
+ if (upload.state == 'done') {
+ window.clearTimeout(options.timer);
+ options.complete(upload);
+ }
+ }
+ });
+};
+
+})(jQuery);
Oops, something went wrong.

0 comments on commit 9d8427f

Please sign in to comment.