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>
<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 顯示欄位 - 儲存檔案路徑
{ 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);
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"]| 問題 | 解決方案 |
|---|---|
| IE 下無法觸發選檔 | 確保 file input 未被隱藏,或使用 label 觸發 |
| 上傳後頁面重整 | 檢查 ajaxfileupload 版本,確認 dataType 設定正確 |
| 大檔案上傳失敗 | 調整 web.config maxRequestLength 與 executionTimeout |
| 中文檔名亂碼 | 伺服器端以 UTF-8 編碼處理檔名,或轉碼儲存 |
LigerUI 已停止維護,新專案建議考慮:
LigerUI 文件上傳整合核心在於:正確引入 ajaxfileupload.js、構建隱藏的上傳彈窗 DOM、Grid/Form 欄位正確映射路徑欄位、onBeforeOpen 事件啟動彈窗、ajaxFileUpload 異步提交並回填結果。遷移新專案時建議採用現代上傳庫。