-2

This is my code to generate a zip file; I am unable to upload a .zip file to server, displaying error like [[Promise]] is not a file:

    var zip = new JSZip();

    zip.file("Hello.txt", "Hello World\n");

  /*create a folder*/

    var img = zip.folder("images");

  /*create a file in images folder*/

    img.file("Hello1.txt", "Hello111 World\n");

  /* generate the zip file */

    var content = zip.generateAsync({type:"blob"});

This is the code I tried to upload the zip file but got no response.

var fd = new FormData();
fd.append('fileZip', content);
$.ajax({
      data: fd,
      url: '/plan-upload/1',
      type: 'POST',
      processData: false,
      contentType: false,
      success: function(response) {
        alert("success"); /*$('#text-us').modal('toggle');*/
      }
    });

Now, can we upload this generated zip file to server? If yes, how? If no, why?

Pavan
  • 300
  • 3
  • 16
  • What are you doing to upload the file to the server? – weirdpanda Feb 09 '18 at 12:29
  • `Now, can we upload this generated zip file to server?` Did you actually tried? – Luis felipe De jesus Munoz Feb 09 '18 at 12:35
  • `an we upload this generated zip file to server?` yes. `how?` - I recommend to start looking here: [**how to upload a zip file to a server using javascript**](https://www.google.com/search?q=how+to+upload+a+zip+file+to+a+server+using+javascript&rlz=1C1CHBD_enIE751IE751&oq=how+to+upload+a+zip+file+to+a+server+using+javascript&aqs=chrome..69i57.7793j0j7&sourceid=chrome&ie=UTF-8) then try to see if any of those solutions are something you can try out. If you have problems with a specific part in your code during that stage post it here and we check it out. – Nope Feb 09 '18 at 12:37
  • @Pavan, can you please update the question? – weirdpanda Feb 09 '18 at 12:44
  • Try [this](https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) one. – weirdpanda Feb 09 '18 at 12:53

1 Answers1

0

When we generating the zip doing this code

var content = zip.generateAsync({type:"blob"});

In content variable has some object type date like promise or Blob type. So backend code doesn't recognize it as a file,Now do this

var file = new File([content], "name.zip");

Now we can send this file, doing this

var fd = new FormData();
fd.append('fileZip', file);
$.ajax({
  data: fd,
  url: '/plan-upload/1',
  type: 'POST',
  processData: false,
  contentType: false,
  success: function(response) {
    alert("success"); /*$('#text-us').modal('toggle');*/
  }
});
Pavan
  • 300
  • 3
  • 16