簡體   English   中英

Angular:在HTTP中發送兩個以上的arguments獲取請求

[英]Angular: Send more than two arguments in HTTP get request

我正在開發一個應用程序,我想在單擊模板上的按鈕后下載文件。 屏幕上顯示了許多文件,每個文件都有一個單獨的按鈕。 我將數組的索引號發送到 Angular,我想將其作為參數發送到后端。 但是,我還想在我的 arguments 中添加 blob 的 responseType 並添加這兩個 arguments 給我一個錯誤。 這是我的代碼:

Angular

onDownloadFiles(i: any)
{
this.fileToDownload = i;
console.log(this.fileToDownload);

const params = new HttpParams().set('id3', this.fileToDownload);
this.http.get('http://localhost:3000/downloadfile', {params}, {responseType: "blob"}) //I want to add both these
.pipe(map(responseData => {
  return responseData;
}))
.subscribe(response => {

   this.downloadFile(response, ("application/msword" || "application/vnd.openxmlformats- 
   officedocument.wordprocessingml.document"));

})
}

downloadFile(data: any, type: string)
{
let blob = new Blob([data], {type: type});
let url = window.URL.createObjectURL(blob);
let pwa = window.open(url);

if(!pwa || pwa.closed || typeof pwa.closed == "undefined")
{
  alert("Please disable your pop up blocker and try again.");
}
}

http.get 方法不能使用超過兩個 arguments 會出錯。 還有其他方法可以將其發送到后端嗎?

get 方法接受兩個 arguments:

  1. url
  2. options -- 由 headers、params、responseType 和其他字段組成。

在您的代碼中,您將分別傳遞 params 和 responseType。 如果將它們組合成單個 object 就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM