1
私のコントローラで作成したzipファイルをajaxで書き出しようとしています。私はいくつかの関連する質問を見てきましたが、それらはすべてわずかに異なり、私はドットを結びつけることができませんでした。時々、私は何も返されません。他の時、私のアヤックスは窓の場所に行き、すべてが壊れます。 MVCでajaxでエクスポートする
私のAJAX呼び出し:私はしました[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");
}
スタッフ:私のコントローラスニップ:私も応答ストリームを使用してみました
[HttpPost, Authorize]
public ActionResult Export(string jsonModel, int? fileType)
{
MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
return File(workingStream, "application/zip", folderName + ".zip");
}
の
$.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;
},
});
一つの試み見て: Generating excel then downloading , Download excel via ajax mvc 、およびJavaScriptでDownloading zip through asp mvc
あなたのdataType HTMLを送り返してくれますが、実際にファイルを送り返しています。あなたのURLをこの結果に設定してください...基本的にこれは本当に何の意味もありません。 – Liam
あなたの2番目のものがあなたのリダイレクトを返す(再びZIPファイルではありません) – Liam
[ /ダウンロードASP.NET MVC](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Liam