1

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 createEnty.

At last converting the Zipstream to bytes array and defining some response headers like content disposition, content type etc..

Please find post action method code below:

    public ActionResult BulkDownload(string[] checkboxSelected)
    {
        string[] fileItemIDs = checkboxSelected.Distinct().ToArray();
        using (System.IO.MemoryStream zipStream = new System.IO.MemoryStream())
        {
            using (ZipArchive zip = new ZipArchive(zipStream, System.IO.Compression.ZipArchiveMode.Update, true))
            {
                foreach (var fileid in fileItemIDs)
                {
                    try
                    {
                        var lgItemID = Sitecore.Context.Database.Items.GetItem(fileid);
                        Sitecore.Data.Fields.FileField fileField = ((Sitecore.Data.Fields.FileField)lgItemID.Fields["File"]);
                        var mediaItem = fileField.MediaItem;
                        var media = MediaManager.GetMedia(mediaItem);
                        var stream = media.GetStream().Stream;
                        var extension = mediaItem.Fields["extension"].Value;
                        if (String.IsNullOrEmpty(extension)) continue;
                        ZipArchiveEntry zipItem = zip.CreateEntry(mediaItem.Name + "." + extension);
                        using (Stream entryStream = zipItem.Open())
                        {
                            stream.CopyTo(entryStream);
                        }

                    }
                    catch (Exception ex) { }
                }
            }
            byte[] fileBytes = zipStream.ToArray();
            System.Net.Mime.ContentDisposition contentDisposition = new System.Net.Mime.ContentDisposition
            {
                FileName = "Selectedfile.zip",
                Inline = false
            };
           string ContentType = "application/x-zip-compressed";
            //string ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;// It also tried following content type: "application/zip","application/x-zip-compressed"
            Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
            Response.Headers.Add("X-Content-Type-Options", "nosniff");
            return File(fileBytes, ContentType);       
    }

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:

enter image description here

I am not 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.

Is there anything more to configure for zip file or any file download programmatically in Sitecore mvc controller actions?

I welcome your suggestions, Thanks!

2
  • And if you use GET method instead of POST?
    – Marek Musielak
    Commented Aug 29, 2022 at 8:33
  • If I use GET, hit action method like normal call, it also not working, but when i hit GET method like api from browser(api/sitecore/controller/action) then zip download works. But requirement is to hit action method as httppost using ajax, because i am sending some media values to actionmethod.
    – Prem Murmu
    Commented Aug 29, 2022 at 9:27

2 Answers 2

0

From what you wrote I think the problem is not with Sitecore or your action, but the problem is with how it's handled with javacsript.

There is a response returned from API to javascript but it's all up to javascript how it's gonna handle it. It has to be coded in a way that browser starts download (or ask user what to do with a file) action.

That's a general javascript question which is not related to Sitecore. You can find an answer here:

https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post

2
-1

You can try this.

Write it to the HttpContext Response, and flush the Response.

Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment; filename=SitecoreMediaDownload.zip");
Response.BinaryWrite(zipStream.ToArray());
Response.Flush();
Response.Close();

For more details, refer to this blog that shows you the step-by-step process of downloading the media and creating a zip.

https://ericastockwellalpert.wordpress.com/2022/04/19/downloading-multiple-files-from-a-server-as-a-single-zip-file/

Hope this help.

5
  • Thanks Sumit for quick response. I tried, no luck. Browser response network tab show file but no download appearing.
    – Prem Murmu
    Commented Aug 29, 2022 at 8:15
  • Did you use the same approach as described in the link I shared? Commented Aug 29, 2022 at 8:24
  • There is a file in App_Config folder called MimeTypes.config. Can you check if you have any kind of type commented there? Commented Aug 29, 2022 at 8:40
  • I am using exact same code, except few changes, i can see some media types commented here.. I try and update
    – Prem Murmu
    Commented Aug 29, 2022 at 8:56
  • I uncommented mime type "application/zip" and tried, Still no download appearing.
    – Prem Murmu
    Commented Aug 29, 2022 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.