JQUery利用Uploadify外掛程式實現檔案非同步上傳(十一)

來源:互聯網
上載者:User

標籤:

一:簡介:Uploadify是JQuery的一個上傳外掛程式,實現的效果非常好,帶進度顯示 ,且Ajax非同步,能一次性上傳多個檔案,功能強大,使用簡單1.支援單檔案或多檔案上傳,可控制並發上傳的檔案數2.在伺服器端支援各種語言與之配合使用,諸如PHP,.NET,Java……3.通過參數可配置上傳檔案類型及大小限制4.通過參數可配置是否選擇檔案後自動上傳5.易於擴充,可控制每一步驟的回呼函數(onSelect, onCancel……)6.通過介面參數和CSS控制外觀 二:官方下載

http://www.uploadify.com/download/

官方文檔

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

 官方示範

http://www.uploadify.com/demo/ 三:使用的方法:Step1:引入相關js和css<link href="JS/jquery.uploadify-v2.1.0/example/css/default.css"     rel="stylesheet" type="text/css" />    <link href="JS/jquery.uploadify-v2.1.0/uploadify.css"     rel="stylesheet" type="text/css" />     <script type="text/javascript"     src="JS/jquery.uploadify-v2.1.0/jquery-1.3.2.min.js"></script>     <script type="text/javascript"     src="JS/jquery.uploadify-v2.1.0/swfobject.js"></script>     <script type="text/javascript"   src="JS/jquery.uploadify-v2.1.0/jquery.uploadify.v2.1.0.min.js"></script>  Step2:前台HTML代碼
<body>
    <div id="fileQueue"></div>
    <input type="file" name="uploadify" id="uploadify" />
    <p>
      <a href="javascript:$(‘#uploadify‘).uploadifyUpload()">上傳</a>|
      <a href="javascript:$(‘#uploadify‘).uploadifyClearQueue()">取消上傳</a>
    </p>
</body>

 Step3:前台JS
<script type="text/javascript">
        $(document).ready(function()
        {
            $("#uploadify").uploadify({
                ‘uploader‘: ‘JS/jquery.uploadify-v2.1.0/uploadify.swf‘,
                ‘script‘: ‘UploadHandler.ashx‘,
                ‘cancelImg‘: ‘JS/jquery.uploadify-v2.1.0/cancel.png‘,
                ‘folder‘: ‘UploadFile‘,
                ‘queueID‘: ‘fileQueue‘,
                ‘auto‘: false,
                ‘multi‘: true
            });
        });  
    </script>
Step4:後台UploadHandler類的ProcessRequest方法代碼如下:
 HttpPostedFile file = context.Request.Files["Filedata"];   
    string  uploadPath =
        HttpContext.Current.Server.MapPath(@context.Request["folder"])+"\\";  
    if (file != null)  
    {  
       if (!Directory.Exists(uploadPath))  
       {  
           Directory.CreateDirectory(uploadPath);  
       }   
       file.SaveAs(uploadPath + file.FileName);  
        //下面這句代碼缺少的話,上傳成功後上傳隊列的顯示不會自動消失
       context.Response.Write("1");  
    }   
    else  
    {   
        context.Response.Write("0");   
    }  

四:其他屬性
uploader : uploadify.swf 檔案的相對路徑,該swf檔案是一個帶有文字BROWSE的按鈕,點擊後淡出開啟檔案對話方塊,預設值:uploadify.swf。

script :   幕後處理程式的相對路徑 。預設值:uploadify.php

fileDataName :設定一個名字,在伺服器處理常式中根據該名字來取上傳檔案的資料。預設為Filedata

method : 提交方式Post 或Get 預設為Post

folder :  上傳檔案存放的目錄 。  sizeLimit : 上傳檔案的大小限制 。
simUploadLimit : 允許同時上傳的個數 預設值:1 。
buttonText : 瀏覽按鈕的文本,預設值:BROWSE 。
buttonImg : 瀏覽按鈕的圖片的路徑 。
hideButton : 設定為true則隱藏瀏覽按鈕的圖片 。
rollover : 值為true和false,設定為true時當滑鼠移到瀏覽按鈕上時有反轉效果。
width : 設定瀏覽按鈕的寬度 ,預設值:110。
height : 設定瀏覽按鈕的高度 ,預設值:30。
wmode : 設定該項為transparent 可以使瀏覽按鈕的flash背景檔案透明,並且flash檔案會被置為頁面的最高層。 預設值:opaque 。
cancelImg :選擇檔案到檔案隊列中後的每一個檔案上的關閉按鈕表徵圖,

 常用事件
onInit : 做一些初始化的工作。
onSelect :選擇檔案時觸發,該函數有三個參數
event:事件對象。
queueID:檔案的唯一標識,由6為隨機字元組成。
fileObj:選擇的檔案對象,有name、size、creationDate、modificationDate、type 5個屬性。

$("#uploadify").uploadify({
        ‘uploader‘: ‘JS/jquery.uploadify-v2.1.0/uploadify.swf‘,
        ‘script‘: ‘UploadHandler.ashx‘,
        ‘cancelImg‘: ‘JS/jquery.uploadify-v2.1.0/cancel.png‘,
        ‘folder‘: ‘UploadFile‘,
        ‘queueID‘: ‘fileQueue‘,
        ‘auto‘: false,
        ‘multi‘: true,
        ‘onInit‘:function(){alert("1");},
        ‘onSelect‘: function(e, queueId, fileObj)

 { alert("唯一標識:" + queueId + "\r\n" + "檔案名稱:" + fileObj.name + "\r\n" + "檔案大小:" + fileObj.size + "\r\n" + "建立時間:" + fileObj.creationDate + "\r\n" + "最後修改時間:" + fileObj.modificationDate + "\r\n" + "檔案類型:" + fileObj.type ); }

onSelectOnce :在單檔案或多檔案上傳時,選擇檔案時觸發。該函數有兩個參數event,data,data對象有以下幾個屬性:

fileCount:選擇檔案的總數。
filesSelected:同時選擇檔案的個數,如果一次選擇了3個檔案該屬性值為3。
filesReplaced:如果檔案隊列中已經存在A和B兩個檔案,再次選擇檔案時又選擇了A和B,該屬性值為2。
allBytesTotal:所有選擇的檔案的總大小。   onCancel : 當點擊檔案隊列中檔案的關閉按鈕或點擊取消上傳時觸發。該函數有event、queueId、fileObj、data四個參數,前三個參數同onSelect 中的三個參數,data對象有兩個屬性fileCount和allBytesTotal。

fileCount:取消一個檔案後,檔案隊列中剩餘檔案的個數。
allBytesTotal:取消一個檔案後,檔案隊列中剩餘檔案的大小。

onClearQueue :當調用函數fileUploadClearQueue時觸發。有event和data兩個參數,同onCancel 中的兩個對應參數。
onQueueFull :當設定了queueSizeLimit並且選擇的檔案個數超出了queueSizeLimit的值時觸發。該函數有兩個參數event和queueSizeLimit。
onError :當上傳過程中發生錯誤時觸發。該函數有event、queueId、fileObj、errorObj四個參數,其中前三個參數同上,errorObj對象有type和info兩個屬性。
type:錯誤的類型,有三種‘HTTP’, ‘IO’, or ‘Security’
info:錯誤的描述

  onOpen :點擊上傳時觸發,如果auto設定為true則是選擇檔案時觸發,如果有多個檔案上傳則遍曆整個檔案隊列。該函數有event、queueId、fileObj三個參數,參數的解釋同上。

onProgress :點擊上傳時觸發,如果auto設定為true則是選擇檔案時觸發,如果有多個檔案上傳則遍曆整個檔案隊列,在onOpen之後觸發。該函數有event、queueId、fileObj、data四個參數,前三個參數的解釋同上。data對象有四個屬性percentage、bytesLoaded、allBytesLoaded、speed:

percentage:當前完成的百分比
bytesLoaded:當前上傳的大小
allBytesLoaded:檔案隊列中已經上傳完的大小
speed:上傳速率 kb/s

 onComplete:檔案上傳完成後觸發。該函數有四個參數event、queueId、fileObj、response、data五個參數,前三個參數同上。response為幕後處理程式返回的值,在上面的例子中為1或0,data有兩個屬性fileCount和speed

fileCount:剩餘沒有上傳完成的檔案的個數。
speed:檔案上傳的平均速率 kb/s
註:fileObj對象和上面講到的有些不太一樣,onComplete 的fileObj對象有個filePath屬性可以取出上傳檔案的路徑。

 onAllComplete:檔案隊列中所有的檔案上傳完成後觸發。該函數有event和data兩個參數,data有四個屬性,分別為:

filesUploaded :上傳的所有檔案個數。
errors :出現錯誤的個數。
allBytesLoaded :所有上傳檔案的總大小。
speed :平均上傳速率 kb/s

 相關函數介紹uploadifyUpload() 上傳uploadifyClearQueue() 下載 uploadifySettings:可以動態修改上面介紹的那些key值,如下面代碼

$(‘#uploadify‘).uploadifySettings(‘folder‘,‘JS‘);
如果上傳按鈕的事件寫成下面這樣,檔案將會上傳到uploadifySettings定義的目錄中

<a href="javascript:$(‘#uploadify‘).uploadifySettings(‘folder‘,‘JS‘);$(‘#uploadify‘).uploadifyUpload()">上傳</a>
uploadifyCancel:該函數接受一個queueID作為參數,可以取消檔案隊列中指定queueID的檔案。

$(‘#uploadify‘).uploadifyCancel(id);

 在Asp.net中是通過在web.config中加一個配置
 <httpRuntime maxRequestLength="1000000" executionTimeout="300"/>

 具體代碼:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server">    <title>Uploadify</title>    <link href="JS/jquery.uploadify-v2.1.0/example/css/default.css"     rel="stylesheet" type="text/css" />    <link href="JS/jquery.uploadify-v2.1.0/uploadify.css"     rel="stylesheet" type="text/css" />    <script type="text/javascript"     src="JS/jquery.uploadify-v2.1.0/jquery-1.3.2.min.js"></script>    <script type="text/javascript"     src="JS/jquery.uploadify-v2.1.0/swfobject.js"></script>    <script type="text/javascript"   src="JS/jquery.uploadify-v2.1.0/jquery.uploadify.v2.1.0.min.js"></script>    <script type="text/javascript">        $(document).ready(function() {            $("#uploadify").uploadify({                ‘uploader‘: ‘JS/jquery.uploadify-v2.1.0/uploadify.swf‘,                ‘script‘: ‘UploadHandler.ashx‘,                ‘cancelImg‘: ‘JS/jquery.uploadify-v2.1.0/cancel.png‘,                ‘folder‘: ‘UploadFile‘,                ‘queueID‘: ‘fileQueue‘,                ‘auto‘: false,                ‘multi‘: true,                //‘fileExt‘: "*.bmp;*.jpg",                //‘fileDesc‘: "請選擇.bmp或.jpg格式的圖片",                ‘onInit‘: function() { $("#msg").html(""); },                ‘onSelectOnce ‘: function(event, queueId, data) {                    //alert("ok");                    $("#msg").html("共上傳" + data.fileCount + "個檔案,檔案大小共" + data.allBytesTotal + "個位元組!");                },                ‘onCancel‘: function(event, queueId, fileObj, data) {                    $("#msg").html("共上傳" + data.fileCount + "個檔案,檔案大小共" + data.allBytesTotal + "個位元組!");                },                ‘onProgress‘: function(event, queueId, fileObj, data) {                    $("#msg").html("當前完成的百分比:" + data.percentage + ",已經上傳完:" + data.allBytesLoaded + ",總共大小為:" + data.bytesLoaded + ",上傳的速度:" + data.speed + "(kb/s)");                }            });        });      </script></head><body>    <div id="fileQueue"></div>    <input type="file" name="uploadify" id="uploadify" />    <p>      <a href="javascript:$(‘#uploadify‘).uploadifyUpload()">上傳</a>|       <a href="javascript:$(‘#uploadify‘).uploadifyClearQueue()">取消上傳</a>    </p>    <p id="msg"></p></body></html>

 

一般處理頁面:
<%@ WebHandler Language="C#" Class="UploadHandler" %>using System;using System.Web;public class UploadHandler : IHttpHandler {    public void ProcessRequest(HttpContext context)    {        context.Response.ContentType = "text/plain";        context.Response.Charset = "utf-8";        HttpPostedFile file = context.Request.Files["Filedata"];        string uploadPath =            HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";        if (file != null)        {            if (!System.IO.Directory.Exists(uploadPath))            {                System.IO.Directory.CreateDirectory(uploadPath);            }            file.SaveAs(uploadPath + file.FileName);            //下面這句代碼缺少的話,上傳成功後上傳隊列的顯示不會自動消失            context.Response.Write("1");        }        else        {            context.Response.Write("0");        }    }    #region IHttpHandler 成員    public bool IsReusable    {        get { throw new NotImplementedException(); }    }    #endregion}

 

 

JQUery利用Uploadify外掛程式實現檔案非同步上傳(十一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.