Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi Friends,
I have a big problem and i am trying for a day to solve this.
I want to down load files from the server. I have used asp.net mvc 4 solution.

My views code is like below.

JavaScript
$.ajax({
                url: '@Url.Action("DownloadFile", "Inbox")',
                type: 'POST',
                cache: false,
                data: {
                    filePaths: filePaths
                },
                success: function (result) {
                    stopPreLoader();
                },
                error: function (result) {
                    stopPreLoader();
                }
            });


Here filePaths is list of names of files. Further my controller is like below,

C#
[HttpPost]
        public FileResult DownloadFile(string filePaths)
        {
            var pathList = filePaths.Split(',').Where(p => p != "").ToList();
            var path = @"" + pathList[0];
            if (pathList.Count == 1)
            {
                byte[] fileBytes = System.IO.File.ReadAllBytes(path);
                string fileName = "abc.pdf";
                return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Zip, fileName);
            }
            if (pathList.Count > 1)
            {
                var id = Session.SessionID;

                var archive = Server.MapPath("~/archive.zip");
                var temp = Server.MapPath("~/temp");

                // clear any existing archive
                if (System.IO.File.Exists(archive))
                {
                    System.IO.File.Delete(archive);
                }
                // empty the temp folder
                Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f));

                // copy the selected files to the temp folder
                pathList.ForEach(f => System.IO.File.Copy(f, Path.Combine(temp, Path.GetFileName(f))));

                // create a new archive
                ZipFile.CreateFromDirectory(temp, archive);
                
                return File(archive, "application/zip", "archive.zip");
                //return File(archive, MimeMapping.GetMimeMapping(archive));
            }

            return null;
        }


So above archive.zip file is created in the server. But I does not appear download confirmation box on the browser.

Please help me to solve this issue.

What I have tried:

I have searched every where on the internet and tried for a day.
Posted
Updated 5-Dec-16 5:09am

1 solution

You can't do this via ajax, just use a normal form that submits to your DownloadFile action and the browser will take care of the rest. You can submit the form via javascript if you don't want to use a submit button.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900