2017-05-25 45 views
-1

我試圖顯示一個成功的AJAX調用後,我的控制器的PDF。我需要修改原來的方式,以便能夠處理多個PDF以返回單個PDF。AJAX調用控制器後顯示PDF

老辦法:

var uri = '../api/Controller/' + string1 + "," + string2 + "," + string3; 
var src = '../web/printViewer.html?file=' + encodeURIComponent(uri); 
window.open(src, "_blank"); 

所以我一直試圖保持的該功能與AJAX調用,看起來像這樣:

$.ajax({ 
    type: 'get', 
    url: '../api/Controller', 
    contentType: 'application/json; charset=utf-8', 
    data: { thing1: item, thing2: item2 }, 
    datatype: 'json', 
    success: function (result) { 
     // no bueno D: 
     // but I get back a proper PDF every time 
    }, 
    error: function() { 
     toastr.error('Error creating print.'); 
    } 
}); 

和C#的控制器:

[HttpGet] 
public HttpResponseMessage Get(string thing1, string thing2) //thing2 will be a string[] eventually, once I am able to get 2-3 pdfs working 
{ 
    byte[] data = response.PDFBytes[0] == null ? new byte[0] : response.PDFBytes[0]; 
    int length = response.PDFBytes[0] == null ? 0 : response.PDFBytes[0].Length; 

    var stream = new MemoryStream(data, 0, length, true, true); 

    result.Content = new ByteArrayContent(stream.GetBuffer()); 

    result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
    { 
     FileName = "CheckedOutItems.pdf" 
    }; 

    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 
} 

我試圖做的第一件事就是把整個事情變成一個大字符串,但它是開始只有幾個PDF文件太長了。我知道控制器正在返回一個可靠的PDF,但我似乎無法將其顯示在printViewer窗口中。

在此先感謝!

編輯:

我不是試圖下載文件。我只是希望在新窗口中打開它,以便用戶可以打印它。有人在開發人員嘗試下載PDF時現在曾兩次提出過相同的文章。那不是我所追求的。

+0

你想下載或顯示它?什麼是printViewer窗口? –

+0

@teovankot我想展示它。 PrintViewer只是一個不同的窗口,可自動爲用戶提供打印對話框。 –

+0

那麼我只能說不要用AJAX下載文件。這真的很棘手,所以最好用舊的方式。欲瞭解更多信息[檢查此](https://stackoverflow.com/q/4545311/1849444)。 –

回答

0

我最終什麼事有在這種情況下需要做的就是將文件保存到磁盤,並在上一個成功的AJAX調用,然後我會去抓住從光盤上的PDF並顯示,小心再之後從磁盤刪除PDF。我根本不想保存它,但我沒有看到另一種方式。

我來這個解決方案的原因是,新窗口實際上打開之前,它是要控制器來獲取圖像,但是當我做了ajax調用,然後試圖打開它在一個新的窗口,它wouldn打得不錯,這導致了另一個電話到服務器。

$.ajax({ 
    type: 'get', 
    url: '../api/Controller', 
    contentType: 'application/json; charset=utf-8', 
    data: { thing1: item, thing2: item2 }, 
    datatype: 'json', 
    success: function (result) { 
     var uri = '../api/GetPDFOffDisk/ + pdfName; 
     var src = '../web/printViewer.html?file=' + encodeURIComponent(uri); 
     window.open(src, "_blank"); 
    }, 
    error: function() { 
     toastr.error('Error creating print.'); 
    } 
}); 

而且在從服務呼叫接收PDF中的控制器(執行下來後,我創建的MemoryStream):

using (Filestream fs = new File.OpenWrite(_path)) 
{ 
    stream.WriteTo(fs); 
    file.Dispose(); 
} 

在新的控制器,以抓住並從磁盤返回的PDF:

if (File.Exists(_path)) 
{ 
    using (FileStream file = System.IO.File.OpenRead(_path)) 
    { 
     var data = File.ReadAllBytes(_path); 
     int length = data.Length; 
     MemoryStream ms = new MemoryStream(data, 0, length, true, true); 
     result.Content = new ByteArrayContent(ms.GetBuffer()); 
     result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
     { 
      FileName = id + ".pdf" 
     }; 
     result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 

     file.Dispose(); 
    } 
    File.Delete(_path); 
} 
0

Inn的控制器的字節數組(PDF文件字節)保存到臨時文件,使你的控制器返回臨時文件的鏈接,並在另一個選項卡

[HttpGet] 
public HttpResponseMessage Get(string thing1, string thing2) //thing2 will be a string[] eventually, once I am able to get 2-3 pdfs working 
{ 
    byte[] data = response.PDFBytes[0] == null ? new byte[0] : response.PDFBytes[0]; 
    int length = response.PDFBytes[0] == null ? 0 : response.PDFBytes[0].Length; 

    var stream = new MemoryStream(data, 0, length, true, true); 

    result.Content = new ByteArrayContent(stream.GetBuffer()); 

    result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
    { 
     FileName = "CheckedOutItems.pdf" 
    }; 
    string TempPath="~/YourFileTempPath.pdf"; 
    using (FileStream file = new FileStream(Server.MapPath(TempPath), FileMode.Create, FileAccess.Write)) { 
    stream.WriteTo(file); 


    return Json(TempPath,JsonBehavior.AllowGet); 
} 

,並在您的JS代碼

打開
$.ajax({ 
    type: 'get', 
    url: '../api/Controller', 
    contentType: 'application/json; charset=utf-8', 
    data: { thing1: item, thing2: item2 }, 
    datatype: 'json', 
    success: function (result) { 
     window.location.href=result; 
    }, 
    error: function() { 
     toastr.error('Error creating print.'); 
    } 
}); 
+0

下載得到的PDF這看起來很有希望。幾個問題:有沒有辦法做到這一點,而不必保存它?對於Server.MapPath,是否是對MS.SQLServer的引用?對於返回的Json,這個引用是什麼? –

+0

Server.mapPath是Asp.net –

+0

的一部分,如果您的問題已解決,請將答案標記爲已接受,並將其解決 –