我有一個REST API,如果我直接在瀏覽器中查詢,它會返回一個JSON或CSV文件進行下載。我想用Angular HttpModule做同樣的事情。Angular 4 HttpModule如何從獲取請求中返回文件
我的GET請求是簡單的(它有一個查詢字符串參數去指示哪些文件格式返回):
public Submit(request: Request) {
...
return this.http.get(url).subscribe(response => {
//What here
});
}
響應內容類型爲application/octet-stream
。我已經嘗試將響應作爲blob處理,但不起作用。我如何才能通過從服務器到瀏覽器的響應並讓它下載文件?這種方法是一個正在由是響應一個按鈕組件調用服務的一部分,單擊
onClick() {
this._requestService.Submit(request); //This gets the Subscription response from the service, but I want it to let the broswer download the file
}
試過,我得到一個錯誤:'資源'blob:6BB5A3D5-0D0D-4537-B4F3-888D7A70DE57'不允許加載.' – hakenmt