在sharepoint中使用javascript读取txt文件

时间:2016-06-29 15:37:04

标签: javascript html sharepoint

我试图阅读内容为:

的文本文件
<p> copyright 2016 ..... </p>

来自sharepoint库中的文件文本:URL

https://mydomaine.sharepoint.com/sites/sitecollection/mysite/assets/footer.txt

使用javascript并将其注入页脚<footer></footer>的html母版页中 任何解决方案?

1 个答案:

答案 0 :(得分:2)

使用XMLHttpRequest对象访问文本文件的内容相对简单。

(function(){
    var serverRelativeUrlOfMyFile = "/sites/sitecollection/mysite/assets/footer.txt";
    var req = new XMLHttpRequest();
    req.onreadystatechange=handler;
    req.open("GET", serverRelativeUrlOfMyFile, true); // params: (method, url, async)
    req.send();
    function handler(){
        if(req.readyState == 4 && req.status == 200){
            // you can now access the file content via the responseText property
            document.querySelector("footer").innerHTML = req.responseText;
        }
    }
})();

上面的代码大致相当于以下jQuery:

var serverRelativeUrlOfMyFile = "/sites/sitecollection/mysite/assets/footer.txt";
$.ajax({
    url: serverRelativeUrlOfMyFile,
    type: "GET"
}).done(handler);
function handler(data){
    $("footer").html(data);
}