DIV CSS 佈局教程網

兩個實用 jQuery 外掛:jQuery File Upload 與 artDialog
編輯:JQuery特效代碼     

做專案時總會遇到幾個反覆要解決的問題:檔案上傳與對話方塊。這次介紹兩個可以直接拿來用的方案——jQuery File Upload 負責上傳,artDialog 負責彈窗。

一、jQuery File Upload:最小可用寫法

這個外掛功能很強,但最基礎的用法其實只要幾行。屬性 data-url 用來指定接收檔案的後端程式:

<input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
  $('#fileupload').fileupload({
    dataType: 'json',   // 以 JSON 格式接收回應
    done: function (e, data) {   // 上傳結束後執行
      $.each(data.result.files, function (index, file) {
        $('<p/>').text(file.name).appendTo(document.body);
      });
    }
  });
});
</script>

注意那三個依賴腳本不可省略:jquery.ui.widget.js 提供底層 widget 基礎,jquery.iframe-transport.js 則是舊瀏覽器(不支援 XHR 上傳者)的降級方案。

進階設定與事件

要控制檔案大小、類型、縮圖等行為,透過 option 設定即可:

$("#fileupload").fileupload('option', {
  url: 'https://localhost/php/index.php',   // 指定接收端
  // 行動裝置上不產生縮圖,避免記憶體吃緊
  disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
  maxFileSize: 5000000,                     // 單檔上限 5MB
  acceptFileTypes: /(\.|\/)(rar|zip|jpe?g|png|bmp)$/i   // 限定副檔名
});

這個外掛最強的地方在事件模型,幾乎上傳生命週期的每個階段都有對應事件可掛:

$('#fileupload')
  .bind('fileuploadadd',       function (e, data) {/* 選檔後 */})
  .bind('fileuploadsubmit',    function (e, data) {/* 送出前 */})
  .bind('fileuploadsend',      function (e, data) {/* 已送出 */})
  .bind('fileuploadprogress',  function (e, data) {/* 單檔進度 */})
  .bind('fileuploadprogressall',function(e, data) {/* 整體進度 */})
  .bind('fileuploaddone',      function (e, data) {/* 單檔完成 */})
  .bind('fileuploadfail',      function (e, data) {/* 單檔失敗 */})
  .bind('fileuploadalways',    function (e, data) {/* 不論成功失敗 */})
  .bind('fileuploadstart',     function (e)       {/* 全部開始 */})
  .bind('fileuploadstop',      function (e)       {/* 全部結束 */});

做進度條只需要在 fileuploadprogressall 中讀取 data.loaded / data.total 換算百分比。完整的 API 建議直接查閱專案 Wiki:https://github.com/blueimp/jQuery-File-Upload/wiki/API。

二、artDialog:以 iframe 開啟子頁並回傳資料

artDialog 是國人開發的彈窗外掛,文件全中文,上手很快。實務上最常遇到的需求是:用 iframe 開啟一個表單頁,關閉時把填好的資料帶回父頁。

先在父頁開啟對話框:

<script src="jquery.mini.js"></script>
<script src="artDialog.js?skin=default"></script>
<script src="./plugins/iframeTools.js"></script>
<script>
function nihao(){
  art.dialog.open(
    "https://localhost/dialog/index2.html",
    {
      title: "測試的頁面",
      width: 320,
      height: 400,
      close: function(){
        // iframe 關閉時執行
        alert("my god 關閉了");
      }
    }
  );
}
</script>

<body>
<input type="text" id="nihao" value="測試數據"/>
<a href="javascript:void(0)" onclick="nihao()">提交</a>

接著在子頁(index2.html)中反向操作父頁的 DOM,把值寫回去:

<script src="jquery.mini.js"></script>
<script src="artDialog.js?skin=default"></script>
<script src="./plugins/iframeTools.js"></script>
<script>
// 取得開啟這個對話框的母視窗
var origin = artDialog.open.origin;
// 找到父頁上的目標欄位並寫入值
var v = origin.document.getElementById('nihao');
v.value = document.getElementById('chushu').value;
</script>

<body>
<input type="text" id="chushu" value="2222111" />

關鍵是 artDialog.open.origin,它指向開啟對話框的母文件,有了這個入口就能自由讀寫父頁元素。若不急著在關閉前回傳,也可以用外掛內建的 art.dialog.data('key', value) 暫存資料,父頁再用 art.dialog.data('key') 取出,語意更清楚。

兩個外掛搭配起來,一個處理檔案、一個處理互動彈窗,基本能覆蓋後台管理介面的大部分需求。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved