2

I have a view with a generate button. When I click It I am navigating to a Generate method of a controller using ajax call.

generate = function () {  
        $.ajax({
            url: "/franchise/Generate",
            type: "POST",
            data: { id: omega.franchiseInfo.Id(), imagesPath: omega.franchiseInfo.ImagesPath() },

        });
    }

Here is my Generate method:

public ActionResult Generate(int id, string imagesPath)
        {
            // some logic here
            var zipFileName = @"D:\FranchiseGeneration\MyZipFile.zip";
            using (var zip = new ZipFile())
            {
                zip.AddDirectory(@"D:\FranchiseGeneration\Test", "Generation");
                zip.Save(zipFileName);
            }
            return File(zipFileName, "application/zip", "MyZipFile.zip"); 
        }

MyZipFile.zip is created on my hard drive as specified. I expect the user to be prompted to download the zipped file ... but nothing happens. I am rather new to Mvc3 and I am not sure what I am doing wrong. Any suggestions with code samples are welcome. Thank You!

4

2 に答える 2

1

It's an ajax call, it doesn't make sense to return a File in an ajax call... ajax stands for Asynchronous JavaScript and XML.. ok with json ad some other text based things, but to work with binary files you'll need some exta works. In you scenario, I think the best thing to do (the simplest one) is to perform a normal postback, not ajax (or even a simple GET would work).

于 2012-09-21T14:14:11.353 に答える
0

It is not possible to trigger a file download via an ajax request like this.

There are other ways to make something like it happen though.

http://johnculviner.com/post/2012/03/22/Ajax-like-feature-rich-file-downloads-with-jQuery-File-Download.aspx

于 2012-09-21T14:10:56.723 に答える