萬盛學電腦網

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

jQuery LigerUI 文件上傳組件整合實戰指南

依賴引入








       
      

HTML 結構




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: {} 
}

核心 JS 邏輯


let uploadDialog = null;

// 1. 綁定下拉框打開事件
$.ligerui.get('AppendBillPath2').set('onBeforeOpen', function() {
    const billNo = $('#AppendBill').val();
    if (!billNo) {
        $.ligerDialog.warn('請先輸入單號!');
        return false; // 阻止打開
    }
    openUploadDialog(billNo);
});

// 2. 打開上傳對話框
function openUploadDialog(billNo) {
    if (uploadDialog) {
        uploadDialog.show();
        return;
    }
    
    uploadDialog = $.ligerDialog.open({
        target: $('#uploadDialog'),
        title: '上傳掃描件',
        width: 360,
        height: 170,
        top: 170,
        left: 280,
        buttons: [
            { text: '上傳', onclick: doUpload },
            { text: '取消', onclick: () => uploadDialog.hide() }
        ]
    });
}

// 3. 執行上傳
function doUpload() {
    const fileVal = $('#fileUpload').val();
    if (!fileVal) {
        $.ligerDialog.warn('請選擇文件!');
        return;
    }
    
    // 文件類型驗證
    const ext = fileVal.substring(fileVal.lastIndexOf('.')).toLowerCase();
    if (!['.jpg', '.jpeg', '.png', '.gif', '.bmp'].includes(ext)) {
        $.ligerDialog.warn('僅支援 jpg/png/gif/bmp 格式!');
        return;
    }
    
    const billNo = $('#AppendBill').val();
    
    $.ajaxFileUpload({
        url: '../handle/ImageUpload.aspx?billNo=' + encodeURIComponent(billNo),
        secureuri: false,
        fileElementId: 'fileUpload',
        dataType: 'text', // 後端返回純文本路徑
        success: function(data, status) {
            $.ligerDialog.success('上傳成功:' + data);
            $('#AppendBillPath1').val(data); // 隱藏域存路徑
            $('#AppendBillPath2').ligerGetComboBoxManager().setValue(data); // 下拉框顯示
            uploadDialog.hide();
            reloadGrid(); // 刷新列表
        },
        error: function(data, status, e) {
            $.ligerDialog.error('上傳失敗:' + e);
        }
    });
}

後端處理


// ImageUpload.aspx.cs
protected void Page_Load(object sender, EventArgs e) {
    if (Request.Files.Count > 0) {
        HttpPostedFile file = Request.Files[0];
        string billNo = Request.QueryString["billNo"];
        
        // 生成唯一文件名
        string ext = System.IO.Path.GetExtension(file.FileName).ToLower();
        string fileName = billNo + "_" + DateTime.Now.Ticks + ext;
        string savePath = Server.MapPath("/Upload/Images/") + fileName;
        
        // 確保目錄存在
        string dir = System.IO.Path.GetDirectoryName(savePath);
        if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
        
        file.SaveAs(savePath);
        
        // 返回相對路徑供前端使用
        Response.Write("/Upload/Images/" + fileName);
        Response.End();
    }
}

關鍵注意點

  1. ajaxfileupload.js 通過隱藏 iframe 模擬異步上傳,非真正 AJAX
  2. fileElementId 必須與 <input type=file> 的 id 一致
  3. 後端返回純文本,不要包含 HTML 標籤
  4. 大文件上傳建議改用 HTML5 FormData + XMLHttpRequest Level 2
  5. 生產環境需加文件大小限制、類型白名單、防病毒掃描
copyright © 萬盛學電腦網 all rights reserved