Problems with downloading pdf file from web api service

后端 未结 1 1990
有刺的猬
有刺的猬 2021-01-15 22:22

I\'m trying to set up a web api service that searches for a .pdf file in a directory and returns the file if it\'s found.

The controller

public class         


        
相关标签:
1条回答
  • 2021-01-15 23:00

    Your code looks similar to what Ive used in the past, but below is what I typically use:

        Response.AddHeader("content-length", myfile.Length.ToString())
        Response.AddHeader("content-disposition", "inline; filename=MyFilename")
        Response.AddHeader("Expires", "0")
        Response.AddHeader("Pragma", "Cache")
        Response.AddHeader("Cache-Control", "private")
    
        Response.ContentType = "application/pdf"
    
        Response.BinaryWrite(finalForm)
    

    I post this for 2 reasons. One, add the content-length header, you may have to indicate how large the file is so the application waits for the whole response.

    If that doesn't fix it. Set a breakpoint, does the byte array content the appropriate length (aka, 30 million bytes for a 30 MB file)? Have you used fiddler to see how much content is coming back over the HTTP call?

    0 讨论(0)
提交回复
热议问题