progressbar使用flash swfupload多上传与控制

分类:Flash_Flex_AIR| 发布:camnprbubuol| 查看:611 | 发表时间:2010/12/29

我用swfulpad控制多个文件上传。它的工作,现在我需要用progerss bar。 这是我的页面代码:

1<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
1<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
2  Upload File
3 
4  </asp:Content>
01<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
02  <html>
03 
04  <head >
05 
06  <link href="Content/default.css" rel="stylesheet" type="text/css" />
07 
08  <script type="text/javascript" src="swfupload/swfupload.js"></script>
09 
10  <script type="text/javascript" src="Scripts/handlers.js"></script>
11 
12  <script type="text/javascript">
13 
14   var swfu;
15 
16   window.onload = function() {
17 
18   swfu = new SWFUpload({
19 
20   // Backend Settings
21 
22   upload_url: "upload.aspx",
23 
24   post_params: {
25 
26   "ASPSESSID": "1"},

// File Upload Settings file_size_limit: "50 MB", file_types: "*.csv", file_types_description: "CSV files", file_upload_limit: 0, // Zero means unlimited

// Event Handler Settings - these functions as defined in Handlers.js // The handlers are not part of SWFUpload but are part of my website and control how // my website reacts to the SWFUpload events. swfupload_preload_handler: preLoad, swfupload_load_failed_handler: loadFailed, file_queue_error_handler: fileQueueError, file_dialog_complete_handler: fileDialogComplete, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: uploadSuccess, upload_complete_handler: uploadComplete,

// Button settings button_image_url: "content/XPButtonNoText_160x22.png", button_placeholder_id: "spanButtonPlaceholder", button_width: 360, button_height: 22, button_text: ’<span>Select CSV Files </span>’, button_text_style: ’.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }’, button_text_top_padding: 1, button_text_left_padding: 5,

// Flash Settings flash_url: "../../swfupload/swfupload.swf", // Relative to this file flash9_url: "../../swfupload/swfupload_FP9.swf", // Relative to this file

custom_settings: { upload_target: "divFileProgressContainer" },

01// Debug Settings
02  debug: false
03 
04  });
05 
06  }
07 
08 </script>
09 
10 </head>
11 
12 <body>
13 
14  <form action="/FileUpload/Upload" method="post" enctype="multipart/form-data">
15 
16  <div id="swfu_container" style="margin: 0px 10px;">
17 
18  <div>
19 
20  <span id="spanButtonPlaceholder"></span>
21 
22  </div>
23 
24  <div id="divFileProgressContainer" style="height: 75px;"></div>
25 
26  <div id="thumbnails"></div>
27 
28  </div>
29 
30  </form>
31 
32  </body>
33 
34 </html>
35 
36 </asp:Content>

下边是我的upload.aspx页面代码:

_______________________

1<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Upload File </asp:Content>

01<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
02  <html>
03 
04  <head >
05 
06  <link href="Content/default.css" rel="stylesheet" type="text/css" />
07 
08  <script type="text/javascript" src="swfupload/swfupload.js"></script>
09 
10  <script type="text/javascript" src="Scripts/handlers.js"></script>
11 
12  <script type="text/javascript">
13 
14   var swfu;
15 
16   window.onload = function() {
17 
18   swfu = new SWFUpload({
19 
20   // Backend Settings
21 
22   upload_url: "upload.aspx",
23 
24   post_params: {
25 
26   "ASPSESSID": "1"},

// File Upload Settings file_size_limit: "50 MB", file_types: "*.csv", file_types_description: "CSV files", file_upload_limit: 0, // Zero means unlimited

// Event Handler Settings - these functions as defined in Handlers.js // The handlers are not part of SWFUpload but are part of my website and control how // my website reacts to the SWFUpload events. swfupload_preload_handler: preLoad, swfupload_load_failed_handler: loadFailed, file_queue_error_handler: fileQueueError, file_dialog_complete_handler: fileDialogComplete, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: uploadSuccess, upload_complete_handler: uploadComplete,

// Button settings button_image_url: "content/XPButtonNoText_160x22.png", button_placeholder_id: "spanButtonPlaceholder", button_width: 360, button_height: 22, button_text: ’<span>Select CSV Files </span>’, button_text_style: ’.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }’, button_text_top_padding: 1, button_text_left_padding: 5,

// Flash Settings flash_url: "../../swfupload/swfupload.swf", // Relative to this file flash9_url: "../../swfupload/swfupload_FP9.swf", // Relative to this file

custom_settings: { upload_target: "divFileProgressContainer" },

01// Debug Settings
02  debug: false
03 
04  });
05 
06  }
07 
08 </script>
09 
10 </head>
11 
12 <body>
13 
14  <form action="/FileUpload/Upload" method="post" enctype="multipart/form-data">
15 
16  <div id="swfu_container" style="margin: 0px 10px;">
17 
18  <div>
19 
20  <span id="spanButtonPlaceholder"></span>
21 
22  </div>
23 
24  <div id="divFileProgressContainer" style="height: 75px;"></div>
25 
26  <div id="thumbnails"></div>
27 
28  </div>
29 
30  </form>
31 
32  </body>
33 
34 </html>
35 
36 </asp:Content>

 

请参考下面的链接为多文件上传和进度条

http://www.codeproject.com/KB/aspnet/FlashUpload.aspx

http://webdeveloperplus.com/jquery/multiple-file-upload-with-progress-bar-using-jquery/

http://www.uploadify.com/documentation/

 

使用jQuery多个文件上传和进度条

默认浏览器文件输入框不允许多个文件上传选择一下,也不提供任何视觉线索多少选择的文件有上传。我在前面写了关于多个文件上传使用AJAX,但是这并没有提供多种文件选择和一个进度条。 今天我要向你展示如何创建一个多个文件上传表单允许多文件选择用Ctrl班次/钥匙,还可以显示一个进度条的每个选定的文件当他们正在上传。

 

For this tutorial, you will need the all powerful jQuery library(latest version) and SWFUpload jQuery Plugin.

Folder Structure

Place the jQuery library in js folder, extract the SWFUpload jQuery Plugin somewhere and copy the jquery.swfupload.js in the src folder to the js folder. Also, copy the swfupload folder inside vendor folder of SWFUpload jQuery Plugin to js Folder. Here’s what your folder structure should look like.

Folder Structure

Add reference to jQuery library and then jquery.swfupload.js in the head tag of your page.

HTML Structure Since we will be using SWFUpload, we’ll need a container that will act as placeholder for SWFUpload button, and a list to show the queue of files.

01<div id="swfupload-control">
02    <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
03 
04    <input type="button" id="button" />
05 
06    <p id="queuestatus" ></p>
07 
08    <ol id="log"></ol>
09 
10    </div>
11 
12    <div id="swfupload-control">
13 
14     <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
15 
16     <input type="button" id="button" />
17 
18     <p id="queuestatus" ></p>
19 
20     <ol id="log"></ol>
21 
22    </div>
23 
24    CSS Styles
25 
26    Here are some CSS styles to stylize our File Uploader, modify them as you want. Important ones are .progressbar and .progress that define the progress bar for file upload, you can adjust their height and color.
01#swfupload-control p{ margin:10px 5px; font-size:0.9em; } #log{ margin:0; padding:0; width:500px;} #log li{ list-style-position:inside; margin:2px; border:1px solid #ccc; padding:10px; font-size:12px; font-family:Arial, Helvetica, sans-serif; color:#333; background:#fff; position:relative;} #log li .progressbar{ border:1px solid #333; height:5px; background:#fff; } #log li .progress{ background:#999; width:0%; height:5px; } #log li p{ margin:0; line-height:18px; } #log li.success{ border:1px solid #339933; background:#ccf9b9; } #log li span.cancel{ position:absolute; top:5px; rightright:5px; width:20px; height:20px; background:url(’js/swfupload/cancel.png’) no-repeat; cursor:pointer; } #swfupload-control p{ margin:10px 5px; font-size:0.9em; }
02 
03    #log{ margin:0; padding:0; width:500px;}
04 
05    #log li{ list-style-position:inside; margin:2px; border:1px solid #ccc; padding:10px; font-size:12px;
06 
07     font-family:Arial, Helvetica, sans-serif; color:#333; background:#fff; position:relative;}
08 
09    #log li .progressbar{ border:1px solid #333; height:5px; background:#fff; }
10 
11    #log li .progress{ background:#999; width:0%; height:5px; }
12 
13    #log li p{ margin:0; line-height:18px; }
14 
15    #log li.success{ border:1px solid #339933; background:#ccf9b9; }
16 
17    #log li span.cancel{ position:absolute; top:5px; right:5px; width:20px; height:20px;
18 
19     background:url(’js/swfupload/cancel.png’) no-repeat; cursor:pointer; }
20 
21    The JavaScript Code
22 
23    The JavaScript code is a bit bulkier but it is not that difficult to understand as it looks.
001$(function(){  $(’#swfupload-control’).swfupload({  upload_url: "upload-file.php",  file_post_name: ’uploadfile’,  file_size_limit : "1024",  file_types : "*.jpg;*.png;*.gif",  file_types_description : "Image files",  file_upload_limit : 5,  flash_url : "js/swfupload/swfupload.swf",  button_image_url : ’js/swfupload/wdp_buttons_upload_114x29.png’,  button_width : 114,  button_height : 29,  button_placeholder : $(’#button’)[0],  debug: false
002 
003     })  .bind(’fileQueued’, function(event, file){ var listitem=’<li id="’+file.id+’" >’+ ’File: <em>’+file.name+’</em> (’+Math.round(file.size/1024)+’ KB) <span class="progressvalue" ></span>’+ ’<div class="progressbar" ><div class="progress" ></div></div>’+ ’<p class="status" >Pending</p>’+ ’<span class="cancel" >&nbsp;</span>’+ ’</li>’;  $(’#log’).append(listitem);  $(’li#’+file.id+’ .cancel’).bind(’click’, function(){ //Remove from queue on cancel click
004 
005    var swfu = $.swfupload.getInstance(’#swfupload-control’);  swfu.cancelUpload(file.id);  $(’li#’+file.id).slideUp(’fast’);  }); // start the upload since it’s queued
006 
007     $(this).swfupload(’startUpload’);
008 
009     })
010 
011     .bind(’fileQueueError’, function(event, file, errorCode, message){
012 
013     alert(’Size of the file ’+file.name+’ is greater than limit’);
014 
015     })
016 
017     .bind(’fileDialogComplete’, function(event, numFilesSelected, numFilesQueued){
018 
019     $(’#queuestatus’).text(’Files Selected: ’+numFilesSelected+’ / Queued Files: ’+numFilesQueued);
020 
021     })
022 
023     .bind(’uploadStart’, function(event, file){
024 
025     $(’#log li#’+file.id).find(’p.status’).text(’Uploading...’);
026 
027     $(’#log li#’+file.id).find(’span.progressvalue’).text(’0%’);
028 
029     $(’#log li#’+file.id).find(’span.cancel’).hide();
030 
031     })
032 
033     .bind(’uploadProgress’, function(event, file, bytesLoaded){
034 
035     //Show Progress
036 
037     var percentage=Math.round((bytesLoaded/file.size)*100);
038 
039     $(’#log li#’+file.id).find(’div.progress’).css(’width’, percentage+’%’);
040 
041     $(’#log li#’+file.id).find(’span.progressvalue’).text(percentage+’%’);
042 
043     })
044 
045     .bind(’uploadSuccess’, function(event, file, serverData){
046 
047     var item=$(’#log li#’+file.id);  item.find(’div.progress’).css(’width’, ’100%’);
048 
049     item.find(’span.progressvalue’).text(’100%’);
050 
051     var pathtofile=’<a href="uploads/’+file.name+’" target="_blank" >view &raquo;</a>’;
052 
053     item.addClass(’success’).find(’p.status’).html(’Done!!! | ’+pathtofile);
054 
055     })
056 
057     .bind(’uploadComplete’, function(event, file){
058 
059     // upload has completed, try the next one in the queue
060 
061     $(this).swfupload(’startUpload’);  }) }); $(function(){
062 
063     $(’#swfupload-control’).swfupload({
064 
065     upload_url: "upload-file.php",
066 
067     file_post_name: ’uploadfile’,
068 
069     file_size_limit : "1024",
070 
071     file_types : "*.jpg;*.png;*.gif",
072 
073     file_types_description : "Image files",
074 
075     file_upload_limit : 5,
076 
077     flash_url : "js/swfupload/swfupload.swf",
078 
079     button_image_url : ’js/swfupload/wdp_buttons_upload_114x29.png’,
080 
081     button_width : 114,
082 
083     button_height : 29,
084 
085     button_placeholder : $(’#button’)[0],
086 
087     debug: false
088 
089     })
090 
091     .bind(’fileQueued’, function(event, file){
092 
093     var listitem=’<li id="’+file.id+’" >’+
094 
095     ’File: <em>’+file.name+’</em> (’+Math.round(file.size/1024)+’ KB) <span class="progressvalue" ></span>’+
096 
097     ’<div class="progressbar" ><div class="progress" ></div></div>’+
098 
099     ’<p class="status" >Pending</p>’+
100 
101     ’<span class="cancel" >&nbsp;</span>’+
102 
103     ’</li>’;
104 
105     $(’#log’).append(listitem);
106 
107     $(’li#’+file.id+’ .cancel’).bind(’click’, function(){ //Remove from queue on cancel click
108 
109     var swfu = $.swfupload.getInstance(’#swfupload-control’);
110 
111     swfu.cancelUpload(file.id);
112 
113     $(’li#’+file.id).slideUp(’fast’);
114 
115     });
116 
117     // start the upload since it’s queued
118 
119     $(this).swfupload(’startUpload’);
120 
121     })
122 
123     .bind(’fileQueueError’, function(event, file, errorCode, message){
124 
125     alert(’Size of the file ’+file.name+’ is greater than limit’);
126 
127     })
128 
129     .bind(’fileDialogComplete’, function(event, numFilesSelected, numFilesQueued){
130 
131     $(’#queuestatus’).text(’Files Selected: ’+numFilesSelected+’ / Queued Files: ’+numFilesQueued);
132 
133     })
134 
135     .bind(’uploadStart’, function(event, file){
136 
137     $(’#log li#’+file.id).find(’p.status’).text(’Uploading...’);
138 
139     $(’#log li#’+file.id).find(’span.progressvalue’).text(’0%’);
140 
141     $(’#log li#’+file.id).find(’span.cancel’).hide();
142 
143     })
144 
145     .bind(’uploadProgress’, function(event, file, bytesLoaded){
146 
147     //Show Progress
148 
149     var percentage=Math.round((bytesLoaded/file.size)*100);
150 
151     $(’#log li#’+file.id).find(’div.progress’).css(’width’, percentage+’%’);
152 
153     $(’#log li#’+file.id).find(’span.progressvalue’).text(percentage+’%’);
154 
155     })
156 
157     .bind(’uploadSuccess’, function(event, file, serverData){
158 
159     var item=$(’#log li#’+file.id);
160 
161     item.find(’div.progress’).css(’width’, ’100%’);
162 
163     item.find(’span.progressvalue’).text(’100%’);
164 
165     var pathtofile=’<a href="uploads/’+file.name+’" target="_blank" >view &raquo;</a>’;
166 
167     item.addClass(’success’).find(’p.status’).html(’Done!!! | ’+pathtofile);
168 
169     })
170 
171     .bind(’uploadComplete’, function(event, file){
172 
173     // upload has completed, try the next one in the queue
174 
175     $(this).swfupload(’startUpload’);
176 
177     })
178 
179    });
180 
181    Code Explanation
182 
183    Since we are using SWFUpload jQuery Plugin, firstly we initialize it on the placeholder element using the $(’#placeholder’).swfupload({ settings }) function and pass it a settings object that defines various parameters like server side file that will handle uploads, maximum size of the file, number of maximum uploads, allowed file types, button url, button size etc. For complete detail of settings you can use, refer to SWFUpload docs.

At this stage, your file upload script will work but will not show any visual clue to users about the upload. SWFUpload fires certain events that we can use to provide information to user about the file uploads. So, next thing is to bind functions to various events to show required output. That’s what all bind calls are doing. They are pretty much self-explanatory.

Limitations Since this script uses SWFUpload to upload files, which requires flash plugin, so this will not work if flash plugin is not installed in end user’s browser. Make sure to provide an alternative upload form so that users without flash can upload files.

Also, SWFUpload has some known issues when used on linux systems and if user is behind proxies. Make sure to read them before you use it in production environment.

View Working Demo or Download Source Code

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/153.html