2016-07-02 3 views
4

メモリPDFファイルを作成するMVCアクションを呼び出します。私はファイルを返し、アクションを完了した直後にダウンロードしたい。MVCアクションを呼び出してPDFファイルをダウンロードするにはどうすればよいですか?

Ajaxコードは、MVCのアクションを呼び出すため

function convertToPDF() { 
     $.ajax({ 
      url: "/Tracker/ConvertPathInfoToPDF", 
      type: "GET", 
      data: JSON.stringify({ 'pInfo': null }), 
      dataType: "json", 
      traditional: true, 
      contentType: "application/json; charset=utf-8", 
      success: function (data) { 

      }, 
      error: function() { 
       alert("Unable to call /Tracker/ConvertPathInfoToPDF"); 
      } 
     }); 
    } 

MVCアクション

public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo) 
    { 
     MemoryStream fs = new MemoryStream(); 
     //FileStream fs = new FileStream(@"C:\Test.pdf", FileMode.Create, FileAccess.Write, FileShare.None); 
     Rectangle rec = new Rectangle(PageSize.A4); 
     Document doc = new Document(rec); 
     PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
     doc.Open(); 
     doc.Add(new Paragraph("Hamed!")); 
     doc.Close(); 

     return File(fs, System.Net.Mime.MediaTypeNames.Application.Octet, "Test.pdf"); 
    } 

MVCアクションがcompeletly実行するが、私は、ブラウザで次のエラーが表示されます。

リソースの読み込みに失敗しました:th MVCのアクションを呼び出すために

public FileResult ConvertPathInfoToPDF(PositionInfo[] pInfo) 
    { 
     MemoryStream fs = new MemoryStream(); 
     Rectangle rec = new Rectangle(PageSize.A4); 
     Document doc = new Document(rec); 
     PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
     doc.Open(); 
     doc.Add(new Paragraph("Hamed!")); 
     doc.Close(); 

     byte[] content = fs.ToArray(); // Convert to byte[] 

     return File(content, "application/pdf", "Test.pdf"); 
    } 

Ajaxコード:Eサーバは500(内部サーバーエラー)の状態

+1

可能な複製MVCコントローラからダウンロードするファイル?](http://stackoverflow.com/questions/730699/how-can-i-present-a-file-for-download-from-an-mvc-controller) –

答えて

4

MVCアクションで応答し

function convertToPDF() { 
     window.location = '/Tracker/ConvertPathInfoToPDF?pInfo=null'; 
    } 
私はどのように提示することができます[の
関連する問題