You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于jQuery的SharePoint多文件上传代码在IE中无法运行求助

修复SharePoint多文件上传代码在IE中的兼容性问题

我帮你梳理了这段代码在IE中失效的核心原因——毕竟IE对现代JavaScript特性的支持很有限,下面是针对性的修复方案和完整的修正代码:

主要兼容问题点及修复思路

  • ES6 let 关键字不支持:IE11及更早版本完全不支持let,需要全部替换为var。
  • FileReader自定义属性传递问题:IE中直接给FileReader实例添加自定义属性可能失效,改用闭包包裹回调的方式稳妥传递文件名。
  • ArrayBuffer转Base64的兼容性:原代码中Uint8Array转字符串再用btoa的方式在IE中处理部分特殊文件可能出错,调整转换逻辑并兼容IE特有的处理方法。
  • jQuery版本适配:确保使用的jQuery版本支持IE(推荐jQuery 1.x或2.x分支,3.x放弃了对IE8及以下的支持)。

修正后的完整代码

jQuery(document).ready(function() { 
    fileInput = $("#getFile"); 
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', registerClick); 
}); 

function registerClick() { 
    //Register File Upload Click Event 
    $("#addFileButton").on('click', readFile); 
} 

var arrayBuffer; 

function readFile() { 
    //Get File Input Control and read the file name 
    var element = document.getElementById("getFile"); 
    var fileCount = element.files.length; 
    var filesUploaded = 0; 

    for (var i = 0; i < fileCount; i++) { 
        var file = element.files[i]; 
        var fileName = file.name; // 提前保存当前文件名
        var reader = new FileReader(); 

        // 用闭包包裹回调,确保循环中能正确获取对应文件的文件名
        reader.onload = (function(targetFileName) {
            return function(e) {
                uploadFile(e.target.result, targetFileName);
            };
        })(fileName); 

        reader.onerror = function(e) { 
            alert(e.target.error); 
        }; 

        reader.readAsArrayBuffer(file); 
    } 
} 

function uploadFile(arrayBuffer, fileName) { 
    //Get Client Context,Web and List object. 
    var clientContext = new SP.ClientContext(); 
    var oWeb = clientContext.get_web(); 
    var oList = oWeb.get_lists().getByTitle('Comms Shared Files'); 

    // 调整ArrayBuffer转Base64的逻辑,适配IE
    var binary = '';
    var bytes = new Uint8Array(arrayBuffer);
    var len = bytes.byteLength;
    for (var j = 0; j < len; j++) {
        binary += String.fromCharCode(bytes[j]);
    }
    var base64;
    // 兼容IE中btoa无法处理非ASCII字符的情况
    try {
        base64 = btoa(binary);
    } catch (e) {
        base64 = window.msConvertBinaryToString ? window.msConvertBinaryToString(binary) : btoa(binary);
    }

    //Create FileCreationInformation object using the read file data 
    var createInfo = new SP.FileCreationInformation(); 
    createInfo.set_content(base64); 
    createInfo.set_url(fileName); 

    //Add the file to the library 
    var uploadedDocument = oList.get_rootFolder().get_files().add(createInfo); 

    //Load client context and execute the batch 
    clientContext.load(uploadedDocument); 
    clientContext.executeQueryAsync(QuerySuccess, QueryFailure); 
} 

function QuerySuccess() { 
    alert('File Uploaded Successfully.'); 
} 

function QueryFailure(sender, args) { 
    console.log('Request failed with error message - ' + args.get_message()); 
}

额外注意事项

  • 确保你的页面引用了兼容IE的jQuery版本,比如<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>。
  • 测试时优先用IE11,更低版本的IE(比如IE10及以下)对SP JSOM的支持可能还有其他限制,必要时需要额外适配。

内容的提问来源于stack exchange,提问作者TonyT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:02:34