简体   繁体   English

创建了Zip文件,但未下载MVC C#

[英]Zip file created but not downloaded MVC C#

In my view i have some checkboxes and their values include a Uri. 我认为我有一些复选框,它们的值包括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. 看起来该zip文件已创建,但是当我尝试下载时,它永远无法正常工作。 I do not get any error. 我没有任何错误。 even more worse i get Status 200. 更糟糕的是,我得到了状态200。

AJAX in View. 视图中的AJAX。

 $(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 请通过浏览器在状态下查找 什么浏览器返回状态200

If I understand your problem correctly, you are trying to download files from an ajax call. 如果我正确理解了您的问题,则您正在尝试从ajax调用下载文件。 It's usually tricky to download files to the client's computer only from javascript. 仅从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/ 使用第三方插件,例如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 /)) @using(Html.BeginForm(“ DownloadAll”,“ AzureSearchService”,/ *其他参数/))
    { {
    @ your checkboxes go here *@ @您的复选框在这里* @
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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