Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m opening the the word pdf txt file from the server from the grid view(Web application) it open fine in visual studio but when i run this code from the iis, it open first time in new window perfectly but another time when i open it that open in same window and shows some machine type code




C#
String path = Server.MapPath(strRequest);
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);

                //Response.End();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            else
            {
                lblError.Text = "This file does not exist.";
            }
Posted

1 solution

using System.Net;

string pdfPath = Server.MapPath("~/SomePDFFile.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(pdfPath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);


or

Response.Redirect("~/somePDFFile.pdf");
 
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