Thursday, July 10, 2025

Response.WriteFile or Response.TransmitFile use in asp.net c# - how to view pdf or text file in web browser

 It is used to show the content of any file in web browser.  This  is same as response.write but it will write the content of the file to web page. Mainly it is used to view the file content not to download the file.  Both Response.WriteFile or Response.TransmitFile  are same

example if text file than use code below

For PDF file

Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;

Response.WriteFile(Server.MapPath("~/App_Data/Annexure.pdf"));


For txt file

Response.Clear(); 

     

        Response.ContentType = System.Net.Mime.MediaTypeNames.Text.Plain;  and use Text.html

        //"application/octet-stream"; 

        Response.WriteFile(Server.MapPath("~/App_Data/yash.txt"));

        Response.End(); 



or on page load, simply we can show the content of text file

Response.WriteFile("~/App_Data/" + fileName);

No comments:

Post a Comment