ASP.NET MVC 4 FileResult - In error

后端 未结 4 1627
我寻月下人不归
我寻月下人不归 2021-02-19 20:35

I have a simple Action on a controller which returns a PDF.

Works fine.

public FileResult GetReport(string id)
{
    byte[] fileBytes = _manager.GetRepor         


        
4条回答
  •  不要未来只要你来
    2021-02-19 21:28

    The FileResult class inherits from ActionResult. So, you can define your Action like this:

    public ActionResult GetReport(string id)
    {
        byte[] fileBytes = _manager.GetReport(id);
        string fileName = id + ".pdf";
    
        if(fileBytes == null || fileBytes.Length == 0)
           return View("Error");
    
        return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
    }
    

提交回复
热议问题