I have problem when I run script it can download zip file but when open zip file it show error "The archive is either in unknow format or damaged"
Before write this script I try Webservice in Postman and use save responce "save to a file " i can get zip file and can use it
This is my code
javascript:
<a id="download-link" style="display: none;">Download</a>
<button id="get-token-button">Get OAuth 2.0 Token</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$("#get-token-button").click(function() {
$.ajax({
url: "https://localhost:9445/test-org/api-test/oauth-test-provider/oauth2/token",
type: "POST",
data: {
grant_type: "client_credentials",
client_id: "client_id",
client_secret: "client_secret",
scope: "scope"
},
success: function(response) {
alert("Token: " + response.access_token);
$.ajax({
url: "https://localhost:9445/test-org/api-test/API/v1/ReceiptService/getReceiptByDataDate",
type: "POST",
headers: {
"Authorization": "Bearer " + response.access_token
},
data: JSON.stringify({
"dataDate": "Date",
"ogaTaxNum": "TaxNum"
}),
contentType: "application/json",
responseType: "arraybuffer",
success: function(response) {
alert(response);
// If the request is successful, create a blob from the response and create a download link
var blob = new Blob([response], {type: "application/zip"});
var url = window.URL.createObjectURL(blob);
$("#download-link").attr("href", url);
$("#download-link").attr("download", "file.zip");
$("#download-link").show();
},
error: function(jqXHR, textStatus, errorThrown) {
// If the request fails, display an error message
alert("Download failed: " + errorThrown);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
// If the request fails, display an error message
alert("Token request failed: " + errorThrown);
}
});
});
</script>
I want solution how to download zip file