Storing files into zip archive and download zip file http post action method and file return type in Sitecore MVC.
Prem Murmu
on
1/5/2023 3:06:33 PM
Requirement: Download selected files or download all. So selected files or all files must be downloaded as a zipped file.
I am going to implement in Sitecore MVC.
Step1: I will write ajax post request that send some media data as array to Action method
$.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);
}
})
Let me explain you what i am doing in ajax call:
type: "POST" : It is post ajax post type call/action
url: "/Files/BulkDownload": It is url to target the controller and action method i am targeting to send data from ajax. I have registered the route thats why only controller and action method name here, but if you don't register the route you can write url like "api/sitecore/{controller name}/{action method name}"
data: JSON.stringify(selectedMediaValues): Converting data to json type
contentType: "application/json; charset=utf-8": It is data type we are sending a data to target action method.
xhrFields: { responseType: 'blob' }: It is data type we are expecting a data after response.
success: function (blob, status, xhr): It is part where we write something after response is ok or status is 200.
Script in this section is to force file download from client(browser).
What script is doing:
It is getting filename from the response header content disposition, based on it, It creating temporary link(anchor link) to download the response file.
It is script I got from Stackoverflow suggested by one of the Sitecore stackexchange's member. It is I will attach link as reference below.
What problem is solve by this script:
Returning file type response from action method in Sitecore mvc was not able to download the file or Zip file, this script helps to download the file.
Step 2. I will write http post actionresult type action method and return type zip file to download.
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(after extracting the media item from sitecore file item) into zip createEnty. Zip acrchive has method to create/update new entry by method createEntry.
At last converting the Zipstream to bytes array and defining some response headers like content disposition, content type etc..
Few notable things:
Get item from ID in Sitecore:
var lgItemID = Sitecore.Context.Database.Items.GetItem(fileid);
Get Media file Field ID in Sitecore
Downloading multiple files from a server as a single zip file
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)