Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to create and showing a pdf from my webpage by clicking a link. But when the system has to show it goes something wrong.

When I use this command

return new FileStreamResult(fileStream, "application/pdf");

It shows a empty pdf file:

///C:/Users/Me/Downloads/C--Apps-MyWebSolution-MyWeb-Documenten-MyList_198721.pdf

It is saved as: C:AppsMyWebSolutionMyWebDocumentenMyList_198721%20.pdf

And when I use this command:

return File(fileStream, "application/pdf", fullFileName);

then I get an error message:

ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION

What am I doing wrong?

public FileStreamResult PDFGenerator(string html, string fileName)
{
    string fullFileName = Server.MapPath("~/Documenten/" + fileName + ".pdf");
    Stream fileStream = CreatePDF(html, fullFileName);

    HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + fullFileName);

    //return new FileStreamResult(fileStream, "application/pdf");
    return File(fileStream, "application/pdf", fullFileName);
}

If I had to post more. I'll do it. But I don't think it is needed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

Most likely you need to .Seek stream to the beginning (usually when you write to a file Position points to end/last written location).

Second issue is somewhat self explanatory as you set "content-disposition" header twice - manualy and via File(...).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...