Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - Duplicate headers and Calling the file in the wrong path

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

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

1 Reply

0 votes
by (71.8m points)

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(...).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...