订阅在Angular 6中的for循环中被调用随机次数

时间:2019-04-25 09:06:00

标签: angular angular6 angular-services subscribe asp.net-core-2.2

我正在用XML导出两个(或多个)文件。为此,我用C#编写了代码,以从后端获取数据并将其导出为XML文件。第一次加载页面后,如果我要导出多个文件,它可以正确导出,但是如果我尝试导出多个文件,则不重新加载页面,它可以导出随机数的带有正确数据的所选文件。一切都发生在“ for循环”中。我一直在寻找解决方案,但没有找到解决方案。很少有人告诉退订,很少有人告诉使用fork join。我已经尝试过,但是没有任何工作对我有用。我不确定我的代码到底在哪里错误。有人可以帮我吗?

下面是我的代码:

onExport(event: Event): void {
    const selectedRecord = Object.assign({}, this.selection.selected);
    console.log(selectedRecord, 'Sample selectedData');
    var SampleIds = { Ids: [] }
    for (var i = 0; i < this.selection.selected.length; i++) {
      SampleIds.Ids.push(this.selection.selected[i].id);
    }
    this.getSampleById(SampleIds.Ids);
}

fileNames = [];
  getSampleById(ids: string[]) {
    this.sampleService.getById(ids)
      .subscribe(data => {
        this.selectedSample = Object.assign([], data);
        const url = environment.api_url_sample + 'Sample/ExportSample/';
        this.selectedSample.forEach(element => {
          const date = new Date();
          this.fileName = element.name + '_' + date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
          this.fileNames.push(this.fileName)
          var downloadData;
          console.log(this.fileNames, 'first downloadData')

          this.httpClient.post(url, element, { responseType: 'text' }).subscribe((data) => {

            downloadData = data;
            console.log(data, 'second downloadData')

            const binaryData = [];
            binaryData.push(downloadData);
            const downloadURL = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/xml' }));
            const link = document.createElement('a');
            link.href = downloadURL;
            console.log(this.fileNames,'inside subscribe');
            link.download = element.name + '.xml';
            link.click();
          },
            error => { alert(error.message); }
          );
        }); 
      }, err => {
        alert("Error in getSampleById")
      });
  }

以下是我通过上述方法调用的服务:

getById(ids : string[]): BehaviorSubject<Sample[]> {
    var sampleIds =
    {
      Ids : ids
    }
    this.apiService.post(environment.api_url_sample, 'sample/GetSamples',sampleIds)
        .subscribe(data => {
          this.selectedSample = data;
          this.selectedSampleSubject.next(this.selectedSample);
        });
    return this.selectedSampleSubject;
  }

下面是我使用.Net Core的C#代码:

        [HttpPost]
        [Route("ExportSample")]
        public async Task<IActionResult> ExportToXml(ResponseSampleDto responseSampleDto)
        {

            List<SummaryDto> Summary = await _measurementValueService.GetMeasurementValuesForExport(responseSampleDto.Id);
            SampleExportDto sampleExportDto = Mapper.Map<SampleExportDto>(responseSampleDto);
            sampleExportDto.Summary = Summary;
            Guid userId = GetCurrentUserId();
            var userDetails = await _userService.GetByIdAsync(userId);
            sampleExportDto.Sample.UpdatedbyName = userDetails.FirstName + " " + userDetails.LastName;
            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(sampleExportDto.GetType());
            MemoryStream memStream = new MemoryStream();
            writer.Serialize(memStream, sampleExportDto);
            memStream.Position = 0;
            return File(memStream, "application/octet-stream");
        }

有人可以帮我吗?

0 个答案:

没有答案