萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

LigerUI 文件上傳元件整合完整實作指南

LigerUI 文件上傳架構概覽

LigerUI 是基於 jQuery 的企業級 UI 框架,其文件上傳功能結合 ajaxfileupload.js 實現無刷新上傳。整合需三大部分:引入腳本、頁面結構、邏輯實作。

一、必要腳本引入

<!-- 核心依賴 -->
<script src="../lib/js/ajaxfileupload.js" type="text/javascript"></script>
<script src="../lib/js/ligerui.expand.js" type="text/javascript"></script>

<!-- LigerUI 核心 (若未全域引入) -->
<link href="../lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" />
<script src="../lib/jquery/jquery-1.5.2.min.js"></script>
<script src="../lib/ligerUI/js/ligerui.min.js"></script>
<script src="../lib/ligerUI/js/plugins/ligerForm.js"></script>
<script src="../lib/ligerUI/js/plugins/ligerGrid.js"></script>

二、上傳彈窗 HTML 結構

<div id="AppendBill_Div" style="display:none;">
  <table style="height:100%;width:100%">
    <tr style="height:40px">
      <td style="width:20%">圖標:</td>
      <td>
        <input type="file" style="width:200px" id="fileupload" name="fileupload" />
      </td>
    </tr>
  </table>
</div>

三、Grid 與 Form 欄位定義

// Grid 顯示欄位 - 儲存檔案路徑
{ display: "掃描件", name: "AppendBillPath", width: 120, type: "text", align: "left" }

// Form 編輯欄位 - 隱藏欄位儲存路徑、下拉框觸發上傳
{ name: "AppendBillPath1", type: "hidden" },  // 路徑儲存
{ 
  display: "掃描件", 
  name: "AppendBillPath", 
  comboboxName: "AppendBillPath2", 
  newline: true, 
  labelWidth: 100, 
  width: 150, 
  space: 30, 
  type: "select", 
  options: {} 
}

// 綁定下拉框開啟前事件
$.ligerui.get("AppendBillPath2").set('onBeforeOpen', f_selectAppendBillPath_1);

四、核心 JavaScript 邏輯實作

var AppendBillPathDetail = null;

function f_selectAppendBillPath_1() {
  var billNo = $("#AppendBill").val();  // 單據號碼
  
  if (!billNo) {
    LG.showError("請先輸入單號!");
    return;
  }
  
  if (AppendBillPathDetail) {
    AppendBillPathDetail.show();
  } else {
    AppendBillPathDetail = $.ligerDialog.open({
      target: $("#AppendBill_Div"),
      title: '添加圖標',
      width: 360,
      height: 170,
      top: 170,
      left: 280,
      buttons: [
        { text: '上傳', onclick: function() { AppendBillPath_save(); } },
        { text: '取消', onclick: function() { AppendBillPathDetail.hide(); } }
      ]
    });
  }
}

function AppendBillPath_save() {
  var filePath = $("#fileupload").val();
  var ext = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
  
  // 檔案類型驗證
  var allowedExts = [".jpg", ".jpeg", ".png", ".gif", ".bmp"];
  if (allowedExts.indexOf(ext) === -1) {
    LG.showError("請上傳 jpg、jpeg、png、gif、bmp 格式圖片");
    return;
  }
  
  var billNo = $("#AppendBill").val();
  
  // 核心上傳邏輯 - 使用 ajaxfileupload
  $.ajaxFileUpload({
    url: "../handle/ImageUpload.aspx?imageurl=" + billNo,
    secureuri: false,
    fileElementId: "fileupload",
    dataType: "text",
    success: function(data) {
      // data 為伺服器返回的檔案路徑或 ID
      if (data && data !== "error") {
        $("#AppendBillPath1").val(data);  // 寫入隱藏欄位
        LG.showSuccess("上傳成功!");
        AppendBillPathDetail.hide();
      } else {
        LG.showError("上傳失敗:" + data);
      }
    },
    error: function(xhr, status, err) {
      LG.showError("上傳異常:" + err);
    }
  });
}

五、伺服器端處理重點

ImageUpload.aspx 需處理:

  • 接收 Request.Files["fileupload"]
  • 驗證檔案大小、類型
  • 生成唯一檔名(建議 GUID + 原副檔名)
  • 儲存至指定目錄
  • 返回相對路徑或檔案 ID 供前端回填

常見問題與解決方案

問題解決方案
IE 下無法觸發選檔確保 file input 未被隱藏,或使用 label 觸發
上傳後頁面重整檢查 ajaxfileupload 版本,確認 dataType 設定正確
大檔案上傳失敗調整 web.config maxRequestLength 與 executionTimeout
中文檔名亂碼伺服器端以 UTF-8 編碼處理檔名,或轉碼儲存

現代化替代方案建議

LigerUI 已停止維護,新專案建議考慮:

  • WebUploader:百度開源,支援分片上傳、斷點續傳
  • DropzoneJS:輕量級拖拽上傳
  • Uppy:模組化、支援多種上傳源
  • 原生 Fetch + FormData:無依賴,現代瀏覽器完美支援

總結

LigerUI 文件上傳整合核心在於:正確引入 ajaxfileupload.js、構建隱藏的上傳彈窗 DOM、Grid/Form 欄位正確映射路徑欄位、onBeforeOpen 事件啟動彈窗、ajaxFileUpload 異步提交並回填結果。遷移新專案時建議採用現代上傳庫。

copyright © 萬盛學電腦網 all rights reserved