使用 Net5 从 restFul Api 自动下载文件

Automatic download file from restFul Api with Net5

我有一个带有 Net5 的 restFul api,在这个 api 我有一个控制器,其中获取对应于文件(pdf、png、jpg ...)稍后必须发送到客户端并在浏览器中自动下载,但是当我从 Swagger 进行测试时,我唯一得到的是:

正如我所见,它无法正常工作,因为我在任何地方都看不到字节数组,而且我只看到 类 的名称。

    [HttpGet("{IdFichero}")]
    public async Task<IActionResult> ObtenerFichero(int IdFichero)
    {
        DbResponseImageHub response = await _unitOfWork.MensajeRepository.ObtenerImagenMensajeRepository(IdFichero);

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(response.fichero);
        result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = "Document " + response.extension;

        switch (response.extension)
        {
            case "png":
                HttpContext.Response.ContentType = "image/png";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                break;
            case "jpg":
                HttpContext.Response.ContentType = "image/png";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
                break;
            case "jpeg":
                HttpContext.Response.ContentType = "image/jpeg";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                break;
            case "bmp":
                HttpContext.Response.ContentType = "image/bmp";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/bmp");
                break;
            case "pdf":
                HttpContext.Response.ContentType = "application/pdf";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                break;
            case "docx":
                HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.documen");
                break;
            case "doc":
                HttpContext.Response.ContentType = "application/msword";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");
                break;
            case "xlsx":
                HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                break;
            case "xls":
                HttpContext.Response.ContentType = "application/vnd.ms-excel";
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
                break;
            default:
                break;
        }

        return Ok(result);
    }

如果我使用 FileResult,它会显示错误“不可调用的 FileResult 成员不能用作方法。”

return FileResult(result);

如果你能告诉我我做错了什么,我将不胜感激。

您必须 return File 反对您的控制器。下面是一个代码示例,它非常接近您要执行的操作(文件对象必须包含文件的字节数组)

    [HttpGet("{IdFichero}")]
    public async Task<IActionResult> ObtenerFichero(int IdFichero)
    {
        string localFilePath = "C:\1639842533574.jpeg";
        string extension = "jpeg";
        var myBytes = System.IO.File.ReadAllBytes(localFilePath);

        switch (extension)
        {
            case "jpg":
                return File(myBytes, "image/jpg");
            case "jpeg":
                return File(myBytes, "image/jpeg");
            default:
                return File(myBytes, "image/gif");
        }
    }