Click here to Skip to main content
16,007,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using following code to show .pdf file in new browser tab:
C#
string fileName = Regex.Replace(Proposal_No, @[/ ], "");
string filePath = Server.MapPath("~/Report_Pdf/Draft_Proposal/") + fileName + ".pdf";

WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(filePath);

if (buffer != null)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", buffer.Length.ToString());
    Response.BinaryWrite(buffer);
}

But this code is not working and instead of downloading .pdf file, .aspx is downloading.

I have also tried following code:
C#
string fileName = Regex.Replace(Proposal_No, @[/ ], "");
string filePath = Server.MapPath("~/Report_Pdf/Draft_Proposal/") + fileName + ".pdf";

Response.ContentType = "application/pdf";
Response.TransmitFile(filePath);

But there is same problem as previous one.
Please help me to get rid of this problem.

Thank u all.
Posted
v3
Comments
Did you try setting Content-Disposition: inline?
F-ES Sitecore 10-Dec-15 6:19am    
Try doing a Response.Clear before and end the response immediately after

http://stackoverflow.com/questions/8897458/asp-net-download-file-to-client-browser

1 solution

C#
private void TestPdf()
    {
        string path = Server.MapPath("~\\C:\\rnd\\test.PDF");
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(path);
        if (buffer != null)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
        }
    }



Its work for me.
When u mapping file make sure I n url / path its returning with .pdf ext.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900