简体   繁体   English

Angular:在HTTP中发送两个以上的arguments获取请求

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

I am working on an application where I want to download a file after clicking on a button on a template.我正在开发一个应用程序,我想在单击模板上的按钮后下载文件。 There are many files that are being displayed on the screen and each file has a separate button.屏幕上显示了许多文件,每个文件都有一个单独的按钮。 I am sending the index number of the array to the Angular which I want to send as params to backend.我将数组的索引号发送到 Angular,我想将其作为参数发送到后端。 But, I also want to add a responseType of blob in my arguments and adding both these arguments is giving me an error.但是,我还想在我的 arguments 中添加 blob 的 responseType 并添加这两个 arguments 给我一个错误。 Here's my code:这是我的代码:

Angular 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.");
}
}

It's giving an error that the http.get method can't take more than two arguments. http.get 方法不能使用超过两个 arguments 会出错。 Is there any other way of sending this to backend?还有其他方法可以将其发送到后端吗?

get method accepts two arguments: get 方法接受两个 arguments:

  1. url url
  2. options -- which consists of the headers, params, responseType and other fields. options -- 由 headers、params、responseType 和其他字段组成。

In your code, you are passing the params and responseType separately.在您的代码中,您将分别传递 params 和 responseType。 If you combine both of them into single object that works.如果将它们组合成单个 object 就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM