Zip file download file response not working in Sitecore MVC Issue resolved
Prem Murmu
on
9/3/2022 4:27:36 PM
Zip file download File type response not working in Sitecore MVC.
Requirement: User can download each file on click download button and download selected files or download all. So selected files or all files must be downloaded as zipped file.
Code:
I created post action result and File return type, which will allow to download zip file.
I am creating memory stream, initiating the ZipArchive Class.
Next putting media files&stream into zip createEntry.
At last converting the ZipStream to bytes array, and defining some response headers like content disposition, content type etc. and forwarding bytes array to download as zip file.
But File return type not able to download the zip file.
Please find post action method code below:
Code//
Code//
No error no warnings, all looks good but not giving download popup or not downloading file.
Please see the browser response request & response header(from network tab) below:
I am was getting why it is able to download zip file. I think Sitecore may be blocking or browser(client) is not able download it.
Because a normal pdf/any media file download functionality also not working in File result type response in Sitecore mvc.
There was my side ,Is there anything more to configure for zip file or any file download programmatically in Sitecore mvc controller actions?
Later i found there is no any fault in Sitecore mvc server side code. I have to handle the file download from the ajax post.
What script will do:
Script in this section is to force the file download from client(browser).
It is getting filename from the response header content disposition, based on it, It creating temporary link(anchor link) to download the response file.
Please find the Ajax post script response section below that worked for in my case.
$.ajax({
type: "POST",
url: "/Files/BulkDownload",
data: JSON.stringify(selectedMediaValues),
contentType: "application/json; charset=utf-8",
xhrFields: {
responseType: 'blob'
},
success: function (blob, status, xhr) {
console.log("Request processed successfully .." + status.responsetext);
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs
//were revoked by closing the blob for which they were created.
//These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location.href = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location.href = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
},
error: function (e) {
alert('Something went wrong while processing your request. Please try again after sometime.');
console.log("ERROR : ", e.responsetext);
}
})
If you wan to see it details please check below link.
If you are using other backend technology, you may get idea from below reference
Handle file download from ajax post
0 Comments on this post
Comments(0)||Login to Comments
- see more..
Categories
ALLAsp.Net (3)
Sitecore (11)
Issues Resolved (9)
Sitecore 9.3 (2)
International Business (1)
Innovations in Industries (3)
Supply Chain Management (1)
Industrial Engineering (14)
Sitecore 10 (4)
Sharepoint (1)
Azure (4)
Cloud Computing (1)
Online Courses (1)
Online Resources (2)
C# (1)
Blazor WebAssembly (2)
Sitecore form (1)
MS Sql Server (1)
Angular 11 (2)
Automobile Engineering (5)
Sitecore powershell (1)
Devops (2)
Microservice Architecture (1)
Sitecore Interview Questions (2)
Dotnet interview questions (1)
Sitecore useful links (1)
Privacy&Cookies (2)
Solr Search (2)
JavaScript (1)
sitecore xm cloud (3)