2016-03-29 3 views
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

+1

あなたのdataType HTMLを送り返してくれますが、実際にファイルを送り返しています。あなたのURLをこの結果に設定してください...基本的にこれは本当に何の意味もありません。 – Liam

+0

あなたの2番目のものがあなたのリダイレクトを返す(再びZIPファイルではありません) – Liam

+0

[ /ダウンロードASP.NET MVC](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Liam

答えて

0

(blockuiとjqueryのファイル・ダウンロードのプラグインを使用して):

コントローラ(ファイルと設定されたCookieを送信)で
 $(window).block(); 
     $.fileDownload('Export', { 
      httpMethod: 'POST', 
      data: inputs, 
      successCallback: function (url) { 
       $(window).unblock(); 
      }, 
      failCallback: function (responseHtml, url) { 
       $(window).unblock(); 
       $(window).html(url + '</br>' + responseHtml); 
      } 
     }); 

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"); 
    } 
関連する問題