locked
Response.contenttype RRS feed

  • Question

  • User-797751191 posted

    Hi

      How the below code works

    Response.Clear();
    string filePath = @"d:\Others\EB.pdf";
    Response.ContentType = "application/pdf";
    Response.WriteFile(filePath);
    Response.End();

    Thanks

    Tuesday, February 5, 2019 3:24 PM

All replies

  • User753101303 posted

    Hi,

    It is just sending the content of a PDF file to the browser. The "content type" allows to tell the other side this is a PDF content (rather than HTML markup). What is done on the client side when a PDF content is received this way may vary (a browser could still decide to show PDF content in his window using a built in or external viewer while another could offer the user to download this file to disk, is this the kind of things you are seeing and want to solve ???)

    Ah and this code runs server side so the file is loaded from the D: drive on the web server. This is a common cause of confusion when starting as developers are using often the same machine for both the client side and the web server which can give a wrong idea about how it works.

    If you need further help please be more explicit about the kind of problem you have with this code.

    Tuesday, February 5, 2019 6:06 PM
  • User-893317190 posted

    Hi jsshivalik,

    Response.Clear() means clearing the previous content in the response in order to keep the response only contains the following data.

    Response.ContentType is used to specify the type of your response, the browser will render your response according to the contentType of your response.

    For example, if I change the contentType to text/html, the browser will render the response as html, if your content is pdf , it will shows content we couldn't read.

       protected void Page_Load(object sender, EventArgs e)
            {
                Response.Clear();
                string filePath = Server.MapPath("/officesample/abc.pdf");
                Response.ContentType = "application/pdf";
                Response.WriteFile(filePath);
                Response.End();
            }

    My client.

    <body>
        <embed src="/officesample/MyPdf.aspx"  width="800" height="800"/>
    </body>

    And the result will be.

    If I change to application/pdf , the browser will render it as pdf.

    Response.WriteFile  could write the file content into the response's outputstream.

    Response.End means ending the response , the following content will not contained in the response. This is also to keep the response only contains the content of your file.

    If you have other question, please specify your problem.

    Best regards,

    Ackerly Xu

    Wednesday, February 6, 2019 4:03 AM