Jason
Jason

Reputation: 1858

Hooking up the download from Ajax "GET"

A zip file will be downloaded on user button click. I have the following setup but I am having trouble how to get these pieces connected.

The "GET" call will return byte content with the content type set as application/x-zip-compressed and content disposition set as attachment with a filename xxxx.zip

$(".downloadBtn").on("click", function(){
  $.ajax({
    headers: {
      "xxxx" : "3.0"
    },
    type: "GET",
    url: url,
    success: function(data) {
      // do something with the data
    },
    error: function(eData) {
      console.log(eData);
    }
  });
});

Upvotes: 0

Views: 64

Answers (2)

Luca Angioloni
Luca Angioloni

Reputation: 2253

Do you have access to the back end? Can you provide more information about the backend part?

Anyway assuming you are using some sort of web APIs to write some sort of front end only application, you could maybe use this Javascript library to create the file from bytes (Blob). It probably won't work on all browsers but it does on chrome, I tried it myself.

Upvotes: 0

Nandan Bhat
Nandan Bhat

Reputation: 1563

This may not be the expected answer, but you can try this as an alternative.

<a href="your_download_url.zip" download>
    <button>Download</button>
</a>

If you are using HTML5, this "download" attribute can be useful.

Upvotes: 2

Related Questions