jQuery模拟弹窗选择文件,以及ajax文件上传

没什么可以说的,直接看代码吧。本来也是自己记录的。

说明:

$.messager,
$.F.refreshSubForm均是自定义方法。不用管
主要看方法:
fileUploadRyxx
// 构建弹窗,选择文件
function fileUploadRyxx( callback, accept = "" ) {
    let a = document.createElement( "input" ) ;
    a.type = "file" ;
    a.accept = accept ;
    a.onchange = ( ) => { if ( a.files.length > 0 ) callback( a.files[0] ) ; } ;
    a.click( ) ;
}

// 导入数据
function cyryxx_import(file){
    var form = new FormData();
    form.append("file", file);
    form.append("formId", "8fea2b0e-f26a-4892-94bd-aa72a2712377");
    form.append("parentRid", "::F8DF01752A392F40FB81");
    form.append("jid", "");
    $.ajax({
        url: "url",
        type: "post",
        data: form,
        async: false,
        processData: false,
        contentType: false,
        success: function(data) {
            $.messager.notice({
                type: "info",
                title: "提示",
                desc: "导入成功",
            });
            $.F.refreshSubForm("F8DF01752A392F40FB81");
        },
        error: function(e) {
            $.messager.notice({
                type: "error",
                title: "错误提示",
                desc: "导入失败," + e.responseJSON.msg,
            });
        }
    });
}
fileUploadRyxx(cyryxx_import);
原文地址:https://www.cnblogs.com/timeout/p/14343532.html