简体   繁体   中英

Zip file created but not downloaded MVC C#

In my view i have some checkboxes and their values include a Uri. In the controller i get the values of the checked checkboxes, download their content (text) and zip them.

It looks like that the zip file is created but when i am trying to download it never works. I do not get any error. even more worse i get Status 200.

AJAX in View.

 $(document).ready(function () {
    $('#GetTotal').on('click', function () {
        var prices = [];
        $('input[name="check[]"]:checked').each(function () {

            prices.push($(this).attr("value"));
        });
        $.ajax({
            url: "/AzureSearchService/DownloadAll",
            type: "GET",
            data: { coursePrices: prices },
            dataType: "json",
            cache: false,
            traditional: true,
            success: function () {
                alert("ajax request to server succeed");
            }
        });
    });
});

The checkbox

<input type="checkbox" name="check[]" value="@url" class="checkbox1" style="margin-left: 5px; margin-right: 5px;">

The Controller

public ActionResult DownloadAll(IEnumerable<Uri> coursePrices)
{
    var outputStream = new MemoryStream();
    using (var zip = new ZipFile())
    {
        foreach (Uri uri in coursePrices)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            Stream s = wc.OpenRead(uri);
            zip.AddEntry(uri.AbsolutePath +  ".txt", s);                 
        }

        zip.Save(outputStream);
    }

    outputStream.Position = 0;
    return File(outputStream, "application/zip", "all.zip");
}

Please find below the status by the browser What Browser Returns Status 200

If I understand your problem correctly, you are trying to download files from an ajax call. It's usually tricky to download files to the client's computer only from javascript. You can consider doing one of the following:

  1. Use a third party plugin like jQuery File Download http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

  2. You can wrap your checkboxes inside a form like so:
    @using(Html.BeginForm("DownloadAll", "AzureSearchService", /* additional parameters /))
    {
    @ your checkboxes go here *@
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM