Read content of SP.File object as text using JSOM

天涯浪子 提交于 2019-12-13 04:33:30

问题


as the title suggests, I am trying to read the contents of a simple text file using JSOM. I am using a Sharepoint-hosted addin for this, the file I am trying to read resides on the host web in a document library.

Here's my JS code:

function printAllListNamesFromHostWeb() {
    context = new SP.ClientContext(appweburl);
    factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);
    appContextSite = new SP.AppContextSite(context, hostweburl);

    this.web = appContextSite.get_web();
    documentslist = this.web.get_lists().getByTitle('Documents');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><ViewFields><FieldRef Name="Name"/></ViewFields></View>');
    listitems = documentslist.getItems(camlQuery);

    context.load(listitems, 'Include(File,FileRef)');

    context.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        var enumerator = listitems.getEnumerator();
        while (enumerator.moveNext()) {
            var results = enumerator.get_current();
            var file = results.get_file();

            //Don't know how to get this to work...
            var fr = new FileReader();
            fr.readAsText(file.get);
        }
    }

    function errorHandler(sender, args) {
        console.log('Could not complete cross-domain call: ' + args.get_message());
    }
}

However, in my succes callback function, I don't know how I can extract the contents of the SP.File object. I tried using the FileReader object from HTML5 API but I couldn't figure out how to convert the SP.File object to a blob.

Can anybody give me a push here?


回答1:


Once file url is determined file content could be loaded from the server using a regular HTTP GET request (e.g. using jQuery.get() function)

Example

The example demonstrates how to retrieve the list of files in library and then download files content

loadItems("Documents",
        function(items) {

           var promises = $.map(items.get_data(),function(item){
               return getFileContent(item.get_item('FileRef'));
           });

           $.when.apply($, promises)
           .then(function(content) {
                   console.log("Done");
                   //print files content
                   $.each(arguments, function (idx, args) {
                       console.log(args[0])
                   });
                },function(e) {
                   console.log("Failed");
                });
        },
        function(sender,args){
            console.log(args.get_message());
        }
    );

where

function loadItems(listTitle,success,error){
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var items = list.getItems(createAllFilesQuery());
   ctx.load(items, 'Include(File,FileRef)');
   ctx.executeQueryAsync(
        function() {
           success(items);          
        },
        error); 
}


function createAllFilesQuery(){
    var qry = new SP.CamlQuery();
    qry.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">0</Value></Eq></Where></Query></View>');
    return qry;
}


function getFileContent(fileUrl){
    return $.ajax({
        url: fileUrl,
        type: "GET"
    });
}


来源:https://stackoverflow.com/questions/37322986/read-content-of-sp-file-object-as-text-using-jsom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!