Skip to main content
Added more details
Source Link
Aakash Maurya
  • 8.5k
  • 5
  • 43
  • 76

Here is the full code:

        function createAndUploadFile()
        {

        var web = context.get_web(); //gets the web object
        var list = web.get_lists(); //gets the list collection object
        var targetList;
        var fileCreation;
        var fileContent;
        targetList = list.getByTitle("Documents");
        fileCreation = new SP.FileCreationInformation();
        var d = new Date()
        var utc = d.toJSON().slice(0, 10) + "-" + d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
        var fileName = "EmployeeList" + utc; //gets the file name
        fileName = fileName + ".xls";
        fileCreation.set_url(fileName);
        fileCreation.set_content(new SP.Base64EncodedByteArray());
        fileContent = str;
        //gets the file contents.
        for (var content = 0; content < fileContent.length; content++) {
        fileCreation.get_content().append(fileContent.charCodeAt(content)); //add the text content to the file.
        }

        //create a new file instance
        var newFile = targetList.get_rootFolder().get_files().add(fileCreation);
        context.load(newFile);
        context.executeQueryAsync(onFileCreationSuccess, onFileCreationFail);

        }

        function onFileCreationSuccess()
        {
        alert("File created and uploaded in document library");
        }

        function onFileCreationFail()
        {
        alert("Error in creating file");
        }

You can call this function on page load or wherever you want.

This worked for me and excel document got uploaded in document library without being downloaded.

This worked for me and excel document got uploaded in document library without being downloaded.

Here is the full code:

        function createAndUploadFile()
        {

        var web = context.get_web(); //gets the web object
        var list = web.get_lists(); //gets the list collection object
        var targetList;
        var fileCreation;
        var fileContent;
        targetList = list.getByTitle("Documents");
        fileCreation = new SP.FileCreationInformation();
        var d = new Date()
        var utc = d.toJSON().slice(0, 10) + "-" + d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
        var fileName = "EmployeeList" + utc; //gets the file name
        fileName = fileName + ".xls";
        fileCreation.set_url(fileName);
        fileCreation.set_content(new SP.Base64EncodedByteArray());
        fileContent = str;
        //gets the file contents.
        for (var content = 0; content < fileContent.length; content++) {
        fileCreation.get_content().append(fileContent.charCodeAt(content)); //add the text content to the file.
        }

        //create a new file instance
        var newFile = targetList.get_rootFolder().get_files().add(fileCreation);
        context.load(newFile);
        context.executeQueryAsync(onFileCreationSuccess, onFileCreationFail);

        }

        function onFileCreationSuccess()
        {
        alert("File created and uploaded in document library");
        }

        function onFileCreationFail()
        {
        alert("Error in creating file");
        }

You can call this function on page load or wherever you want.

This worked for me and excel document got uploaded in document library without being downloaded.

Source Link
Aakash Maurya
  • 8.5k
  • 5
  • 43
  • 76

Hey I have got the same requirement to work with and I was able to accomplish this task so here I am sharing my approach.

  • I created a Excel file using SPFile object available in sp.js

                 var fileCreation = new SP.FileCreationInformation();
                 var d = new Date()
                 var utc = d.toJSON().slice(0, 10) + "-" + d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
                 var fileName = "EmployeeList" + utc; //gets the file name
                 fileName = fileName + ".xls";
                 fileCreation.set_url(fileName);
                 fileCreation.set_content(new SP.Base64EncodedByteArray());
    
  • Then added the list data into that file using JavaScript code

     fileContent = str;  // where str is your data in byte array
     for (var content = 0; content < fileContent.length; content++) {
             fileCreation.get_content().append(fileContent.charCodeAt(content)); //add the text content to the file.
         }
    
  • And uploaded that file directly into the document library again using JavaScript code

                     //create a new file instance
                     var newFile = targetList.get_rootFolder().get_files().add(fileCreation);
                     context.load(newFile);
                     context.executeQueryAsync(onFileCreationSuccess, onFileCreationFail);
    
         function onFileCreationSuccess() {
         alert("File created and uploaded in document library");
         }
         function onFileCreationFail() {
         alert("Error in creating file");
         }
    

This worked for me and excel document got uploaded in document library without being downloaded.

Let me know if you need more help.