簡體   English   中英

使用JS FileReader API讀取Sharepoint文檔庫文件

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

我看到這個問題的其他版本沒有提供Javascript解決方案。 有人可以為此創建一個嗎? 我把頭撞在牆上,試圖使它起作用。

因此,我的總體工作范圍是復制一個文件,然后將其放入另一個文檔庫中,但是我在讀取文件時遇到了麻煩。 到目前為止,這是我最好的嘗試...

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));
                }
            }); 
        }
    });
}

使用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); } } 

我也建議不要使用SPService庫,因為它是第三方庫,並且該庫中的大多數功能都可以在REST api中獲得。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM