如何下载客户端Web应用程序中webAPI收到的ClosedXml文件

在我的webAPI中,我创建一个文件,然后将其发送到我的Aurelia Web应用程序

[HttpPost]
[Route("Print")]
public async Task<IactionResult> Print([FromBody] PrintDataVM data)
{
  var getResponse = await mediator.Send(new PrintPlanning(data));
  return Ok(getResponse.Deliver("Planificacion.xlsx"));

}

我返回的是一个XLWorkBook对象,该文件是作为响应生成的。如果我将其保存在我的webAPI文件夹中的代码(wb.SavAes(“ test.xlsx”))中,以查看结果工作正常

在我的前端代码中,我有这个

async print() {
try {
  let printData = new PrintData(
    this.proyectoSeleccionado,this.fechaInicio,this.fechaFin,this.horas,this.certificaciones,this.subcontrataciones,this.contratos,this.resumen
  );
  this.planningService.Print(printData).then(json => {
    var blob = new File([json],"Planificacion.xlsx");
    var URL = window.URL || window.webkitURL;
    var downloadURL = URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.href = downloadURL;
    a.download = "Planificacion.xlsx";
    document.body.appendChild(a);
    a.click();
  });
} catch (error) {
  console.error(error);
  alert("Ha ocurrido un error generando el Libro de Excel para imprimir");
  return null;
}

}

但是文件下载大小只有15个字节,当我尝试打开它时出现此错误

如何下载客户端Web应用程序中webAPI收到的ClosedXml文件

我尝试生成Blob而不是文件

this.planningService.Print(printData).then(json => {
    const file = new Blob([json],{
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    });
    var URL = window.URL || window.webkitURL;
    var downloadURL = URL.createObjectURL(file);
    var a = document.createElement("a");
    a.href = downloadURL;
    a.download = "Planificacion.xlsx";
    document.body.appendChild(a);
    a.click();
  });

但是当我打开下载的文件时,说Excel无法打开文件,因为格式和扩展名无效。

有什么想法吗?

致谢

mj5an 回答:如何下载客户端Web应用程序中webAPI收到的ClosedXml文件

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160965.html

大家都在问