简体   繁体   English

使用JS FileReader API读取Sharepoint文档库文件

[英]Read Sharepoint Document Library file with JS FileReader API

I see other versions of this question that don't provide a Javascript solution. 我看到这个问题的其他版本没有提供Javascript解决方案。 Can someone create one for this? 有人可以为此创建一个吗? I'm banging my head against the wall trying to get this to work. 我把头撞在墙上,试图使它起作用。

So my overall scope is to copy a file, then put it in another document library, but I'm having trouble just reading the file. 因此,我的总体工作范围是复制一个文件,然后将其放入另一个文档库中,但是我在读取文件时遇到了麻烦。 This is my best attempt so far... 到目前为止,这是我最好的尝试...

function createAPinSPListOld(name,system,start,duration) {

    var binary = "";   
    var srcUrl = can't show this;
    var destURL = can't show this either; 

    // create metadata
    var data = { 
        __metadata: {'type': 'SP.Data.NXE_x0020_AP_x0020_TrackerItem'},
        System: system,
        Planned_x0020_Start: start,
        Required_x0020_Time_x0020__x0028_hrs_x0029_: duration,
    };

    $().SPServices({
        operation: "GetItem",
        Url: srcUrl,
        completefunc: function (xData, Status) {                    
            binary = $(xData.responseXML).find("Stream").text(); 
            console.log(xData);                                      
            console.log(binary);                                      
            // create item in SP list
            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('NXE Action Plan Tracker')/rootfolder/files/add(url="+name+",overwrite=true)",
                type: "POST",
                body: binary,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "content-type": "application/json;odata=verbose",
                    "content-length":500000
                },
                data: JSON.stringify(data),
                success: function (data) {
                    console.log("Created successfully!")
                },
                error: function (error) {
                    alert("cannot add for some reason");
                    alert(JSON.stringify(error));
                }
            }); 
        }
    });
}

Use the xhr to read document into byte array by using following function: 使用xhr通过以下功能将文档读入字节数组:

 function ReadDocument(documentUrl) { try { if (documentUrl) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function (data) { if (this.readyState == 4 && this.status == 200) { var fileData = this.response; } } xhr.open('GET', documentUrl, true); xhr.responseType = 'blob'; xhr.send(); } } catch (e) { console.log(e); } } 

Also I would recommend not to use SPService library as it is third party library and most of the feature that are available in this library are available in REST api. 我也建议不要使用SPService库,因为它是第三方库,并且该库中的大多数功能都可以在REST api中获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM