ASP.NET MVC FileStreamResult, fileDownloadName is not used

前端 未结 3 1406
感动是毒
感动是毒 2020-12-15 08:18

The following returns a PDF which the browser tries to directly display inline. This works correctly. However, if I try to download the file, the download name is not \"myPD

相关标签:
3条回答
  • 2020-12-15 08:50

    No, this is not possible with a PDF displayed inline. You could achieve this if you send the Content-Disposition header with as an attachment:

    public ActionResult PDFGenerator(int id)
    {
        Stream stream = GeneratePDF(id);
        return File(stream, "application/pdf", "myPDF.pdf");
    }
    

    Also notice how I removed the unnecessary MemoryStream you were using and loading the PDF in memory where you could have directly streamed it to the client which would have been far more efficient.

    0 讨论(0)
  • 2020-12-15 08:59

    It is possible by making the id a string which represents the file name without the extension.

    public ActionResult PDFGenerator(string id, int? docid)
    {
        Stream stream = GeneratePDF(docid);
        return new FileStreamResult(stream , "application/pdf");
    }
    

    The url then then end like this

      ..PDFGenerator/Document2?docid=15
    
    0 讨论(0)
  • 2020-12-15 09:03

    If you are using FileStreamResult to download the file, try using this in controller

    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment; filename=FileName.pdf");
    
    0 讨论(0)
提交回复
热议问题