我正在尝试通过ajax导出在我的控制器中创建的zip文件。我看了几个相关的问题,但它们都略有不同,我无法将它们联系起来。有时,我什么也得不到。其他时候,我的ajax转到窗口位置,所有东西都崩溃了。
我的ajax调用:
$.ajax({
url: "/Somewhere/Export",
type: "POST",
dataType: 'html',
data: {
jsonModel: JSON.stringify(modelData),
fileType: $("#toggle-2").val()
},
success: function (returnValue) {
window.location = '/Somewhere/Export/Download?file=' + returnValue;
},
});一次尝试我的控制器剪辑。:
[HttpPost, Authorize]
public ActionResult Export(string jsonModel, int? fileType)
{
MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
return File(workingStream, "application/zip", folderName + ".zip");
}我也尝试过使用响应流:
[HttpPost, Authorize]
public ActionResult Export(string jsonModel, int? fileType)
{
MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + folderName + ".zip");
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Zip;
workingStream.WriteTo(Response.OutputStream);
Response.End();
return RedirectToAction("Index");
}我看过的内容:Generating excel then downloading、Download excel via ajax mvc和Downloading zip through asp mvc
发布于 2016-03-30 00:41:28
在javascript中(使用blockui和jquery-file-download插件):
$(window).block();
$.fileDownload('Export', {
httpMethod: 'POST',
data: inputs,
successCallback: function (url) {
$(window).unblock();
},
failCallback: function (responseHtml, url) {
$(window).unblock();
$(window).html(url + '</br>' + responseHtml);
}
});在控制器中(发送文件和设置cookie):
public ActionResult Export()
{
var e = new ExportZip();
var request = Request.Unvalidated;
byte[] data = e.Create(request.Form);
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
return File(data, "application/zip", "file.zip");
}https://stackoverflow.com/questions/36289120
复制相似问题