none
How to create folder on user desktop using jquery for downloading all documents from document library. RRS feed

  • Question

  • My task is to download all the documents from document library in folder format.

    Documents are downloading one by one individually.

    I require them to be downloaded as a whole in folder format.

     myObject = new ActiveXObject("Scripting.FileSystemObject"); 

    this works only in Internet Explorer to create folder.

    Can anyone suggest me,whether it is possible or not to do this.

    Wednesday, March 15, 2017 7:24 AM

Answers

  • {

    var deferreds = [];

    deferreds.push(addToZip(zip, filename[i], URL)); 

    $.when.apply(window, deferreds).done(function() {
             zip.generateAsync({                                 //After Completion of all the responces,generating zip.
                 type: "blob"
             }).then(function(data) {
                 saveAs(data, "example.zip");
             });
         });

    }

    function addToZip(zip, imgLink, url) {
         var d1 = $.Deferred(); 
         var xhr = new XMLHttpRequest();
         xhr.open('GET', url, true);
         xhr.responseType = "arraybuffer";
         xhr.onreadystatechange = function(evt) {
             if (xhr.readyState === 4) {
                 if (xhr.status === 200) {
                     zip.file(imgLink, xhr.response);
                     d1.resolve(zip);
                 }
             }
         };
         xhr.send();
         return d1;
     }

    This is working fine for me.Hope it may help anyone.

    • Marked as answer by Bindu Kaperla Friday, December 15, 2017 9:31 AM
    Thursday, March 23, 2017 6:41 AM

All replies

  • One option which I had tried is to use jszip javascript library. Programmatically you can read each folder and file from a document library and create a zip file out of it. This library provides you an option to create a folder and add files to that folder. So you will create the same structure as it exists in a library.

    Using FileSaver.js library, you can prompt the zip file for the user to Save it to a desktop. Then user can extract the zip file which will create each folder and file as it exists in the document library.

    This entire solution is a client side code, so you don't need to deploy to the server.

    Wednesday, March 15, 2017 8:48 AM
  •  Thank you for your suggestion.

    But I tried the jszip library for creating folder and download it with this following sample code:

    $(document).ready(function() {
             var zip = new JSZip();
             zip.file("sample.txt", "Hello World\n");
             var img = zip.folder("samplefolder1");
             var content = zip.generateAsync({type:"blob"}).then(function(content){
                 

                    var zipName = 'folder1';
                   var a = document.createElement('a');
                  a.href="data:application/zip;base64,"+content;
                  a.download = zipName;               a.click();
                  })
            });

    But ,its not working.Is there anything wrong with my code. 


    Wednesday, March 15, 2017 9:24 AM
  • What is not working in the code? Are you getting any specific error?

    One more thing in the code is: If you want to add sample.txt file under the folder samplefolder1, then first create the folder, and then while adding a file, provide the path of that folder.

    Try something like this: zip.file("samplefolder1/sample.txt", "Hello World");

    Wednesday, March 15, 2017 10:15 AM
  • Yes,its working fine thank you so much.

    Am able to create folder and add files and am able to download it.Its working fine.

    Am adding file as zip.file("sample.txt", "Hello World");

    i want to provide data dynamically to zip as:

    var zip = new JSZip();
    var img = zip.folder("images");
    img.file("sam.txt","http://ie-dev-sp12:8888/afcent/staff/fm/art/SiteAssets/sam.txt",

    {type: "text/plain;charset=utf-8"});

    Is this the correct way of doing it??


    Wednesday, March 15, 2017 10:54 AM
  • Use JSZipUtils.getBinaryContent method to load files dynamically and add it to zip file. Check this link for details: https://github.com/Stuk/jszip-utils
    Wednesday, March 15, 2017 1:16 PM
  • Thank You,

    But i tried doing in this way,But am getting errors in utils package as:

    undefined method require(),

    I tried in one more way to move my file data into zip by converting file data into blob and then storing.

              var xhr = new XMLHttpRequest();
                xhr.open('GET', _spPageContextInfo.webAbsoluteUrl + '/SiteAssets/sample.txt',true);
                xhr.responseType = "blob";
                xhr.withCredentials = true;
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4) {
                        var blob = xhr.response;
                        zip.file("sample.txt",blob,{base64: true});
                    }
                };
                xhr.send();

    But in this way am able to download the file ,but am not able to store it into zip.

    Thursday, March 16, 2017 6:11 AM
  • Bindu, I have used JSZipUtils.getBinaryContent and it worked for me. Can you check the example provided here?
    Friday, March 17, 2017 7:25 AM
  • Thank you Suresh,

    My functionality is working fine for .txt documents. But, i am facing issues with other file extensions.

    Here is my code for .txt files:

    var  request = $.ajax({
                 data:"",
                    url: URL,       //url of file
                    type: "GET",
                    async: false,
                });                  

    request.done(function(data, status, xhr) {
                    zip.folder(docfolder[i]).file(filename[i], data, {
      binary: true
                    });    
                })

    Here is the code for other extension of files:Am facing issues as corrupted file.

    var  request = $.ajax({
                 data:"",
                    url: URL,       //url of file
                    type: "GET",
                    async: false,
                }); 

     request.done(function(data, status, xhr) {
                    zip.file(filename[i], data, {
                       base64: true
                    });     
                })  

    Can you please suggest me the file formats for other file extensions.

    Monday, March 20, 2017 11:53 AM
  • {

    var deferreds = [];

    deferreds.push(addToZip(zip, filename[i], URL)); 

    $.when.apply(window, deferreds).done(function() {
             zip.generateAsync({                                 //After Completion of all the responces,generating zip.
                 type: "blob"
             }).then(function(data) {
                 saveAs(data, "example.zip");
             });
         });

    }

    function addToZip(zip, imgLink, url) {
         var d1 = $.Deferred(); 
         var xhr = new XMLHttpRequest();
         xhr.open('GET', url, true);
         xhr.responseType = "arraybuffer";
         xhr.onreadystatechange = function(evt) {
             if (xhr.readyState === 4) {
                 if (xhr.status === 200) {
                     zip.file(imgLink, xhr.response);
                     d1.resolve(zip);
                 }
             }
         };
         xhr.send();
         return d1;
     }

    This is working fine for me.Hope it may help anyone.

    • Marked as answer by Bindu Kaperla Friday, December 15, 2017 9:31 AM
    Thursday, March 23, 2017 6:41 AM
  • Bindu, good to know that JSZip library was useful. Thanks for the update.
    Friday, March 24, 2017 3:35 PM
  • Will this code work in IE. I am using similar code to generate zip using jszip, it works fine in Chrome/Firfox/Opera browser.However it doesn't download in IE if the file size is more than 100MB.

    Regards RCP

    Wednesday, September 12, 2018 11:43 AM