I work with Web Api (C#) and angular.js on client. I need to download server response content (ByteArrayContent of zip). I have this method on server:
public HttpResponseMessage Download(DownloadImagesInput input)
{
if (!string.IsNullOrEmpty(input.ImageUrl))
{
byte[] imageBytes = GetByteArrayFromUrl(input.ImageUrl);
ZipManager manager = new ZipManager();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
byte[] zipBytes;
zipBytes = string.IsNullOrEmpty(input.QrCode) ? manager.ZipFiles(imageBytes)
: manager.ZipFiles(imageBytes, input.QrCode);
result.Content = new ByteArrayContent(zipBytes);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/zip");
return result;
}
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
The ZipManager is my Service, it just return the byte array of zip file. I need to download this zip archive on client. This is my client:
$apiService.downloadZip({ 'ImageUrl': $scope.currentImage, 'QrCode': str }).then(function (response) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/zip,' + response.data;
hiddenElement.target = '_blank';
hiddenElement.download = 'images.zip';
hiddenElement.click();
});
Result : download zip file but i can't open it, the file have invalid format
The zip file created on server is ok, i just check it by directly save him from server to disk... Need help.