我有一個帶有ASP.Net Web API的angular2項目。我有代碼從我的數據庫檢索到我的服務器上的文檔的文件路徑。然後我想在瀏覽器中的新標籤中顯示這個文檔。有沒有人有任何建議如何做到這一點?Angular2顯示PDF
我很高興能夠檢索Angular2(Typescript)或我的API中的文件並將其流式傳輸。
這是我在我的API檢索它的嘗試,但我不能工作,如何接受它Angular2並正確顯示:
public HttpResponseMessage GetSOP(string partnum, string description)
{
var sopPath = _uow.EpicorService.GetSOP(partnum, description).Path;
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(sopPath, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
任何幫助將不勝感激。
很多謝謝!
如果您想在另一個選項卡中看到它,爲什麼不直接打開API的新選項卡並讓瀏覽器解釋內容?您可能需要使用MIME類型的「application/pdf」而不是「application/octet-stream」。 – LinuxDisciple
我寫了關於獲取和顯示在Angular 2中的新選項卡中的pdf文件,請查看我的答案:http://stackoverflow.com/questions/37046133/pdf-blob-is-not-showing-content-angular-2/ 39657478#39657478 –
@StefanSvrkota會這樣使用我的API,因爲它是?或者,我需要進行更改才能以正確的格式返回它? – DaRoGa