[英]Zip file created but not downloaded MVC C#
我认为我有一些复选框,它们的值包括Uri。 在控制器中,我获得了选中复选框的值,下载它们的内容(文本)并压缩它们。
看起来该zip文件已创建,但是当我尝试下载时,它永远无法正常工作。 我没有任何错误。 更糟糕的是,我得到了状态200。
视图中的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");
}
});
});
});
复选框
<input type="checkbox" name="check[]" value="@url" class="checkbox1" style="margin-left: 5px; margin-right: 5px;">
控制器
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");
}
请通过浏览器在状态下查找 什么浏览器返回状态200
如果我正确理解了您的问题,则您正在尝试从ajax调用下载文件。 仅从javascript下载文件到客户端计算机通常很棘手。 您可以考虑执行以下操作之一:
使用第三方插件,例如jQuery File Download http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
您可以将复选框包装在如下形式的表格中:
@using(Html.BeginForm(“ DownloadAll”,“ AzureSearchService”,/ *其他参数/))
{
@您的复选框在这里* @
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.