1

In asp.net mvc project how can i work iframe to show a document file? I tried to do below the code like that,but when i run the project file has download...why??

 public FileStreamResult GetPDF()
        {
            FileStream fs = new FileStream(Server.MapPath(@"~/File/SegmentAdd.txt"), FileMode.Open, FileAccess.Read);
            return File(fs,"application/pdf","SegmentAdd.txt");         
        }

      <iframe src="@Url.Action("GetPDF","Home")"  width="90%" height="90%"></iframe>
Mehedi Islam
  • 59
  • 1
  • 12
  • 1
    This depends on the browser settings and the server http headers. See http://stackoverflow.com/questions/6293893/how-to-force-files-to-open-in-browser-instead-of-download-pdf for more details. – TeaCoder Jun 21 '16 at 05:16
  • i don't understand now what should i do?if i use like that... return File(fs,"application/pdf","SegmentAdd.txt"); – Mehedi Islam Jun 21 '16 at 05:29
  • 1
    There's nothing wrong with your code, however it still depends on the browser, does your browser contains a plugin to read pdf files? If it has, then most probably your code will work, but if your browser does not have the plugin then it will fallback to just downloading the pdf. – Bon Macalindong Jun 21 '16 at 05:48
  • Bon Macalindong, I already plugin pdf reader into my chrome browser,but it does not work.It's always downloaded and doesn't show file.Qhat should i do...? – Mehedi Islam Jun 21 '16 at 06:15

1 Answers1

2

Modify your code as follows

public FileStreamResult GetPDF()
{
     FileStream fs = new FileStream(Server.MapPath(@"~/File/SegmentAdd.txt"), FileMode.Open, FileAccess.Read);
     return File(fs, "text/plain");
}

Actually the content type of .txt file is

text/plain

I hope this will help you

UPDATE

Inorder to know the content type of each file types Please refer MIME Type list

Jayaraj.K
  • 928
  • 9
  • 30
  • Thank you,Jayaraj.K It works. But one question when file type like .pdf/.doc or etc. what should be the content type? – Mehedi Islam Jun 21 '16 at 07:39
  • It depend upon your file which you are trying to show. Please refer http://www.freeformatter.com/mime-types-list.html, MIME Type column you can find content type to be used for each file. – Jayaraj.K Jun 21 '16 at 07:51
  • Thank you very much Jayaraj.K – Mehedi Islam Jun 21 '16 at 08:02
  • Hey Jayaraj,how r you? i have a problem in MapPath(" "); when i get file name from database how can i map in the filestream and how can i show on iframe? – Mehedi Islam Jun 22 '16 at 06:59
  • What problem you are facing in MapPath. Does it throws any error, If so post the issue as another question. – Jayaraj.K Jun 22 '16 at 07:02
  • Hey Jayaraj, I can not post new qus cz i have already post another qus. If u don't mind can i describe this prob via mail or social any site or ur personal blog? – Mehedi Islam Jun 22 '16 at 07:15
  • Jayaraj,i already post my qus,please see this link http://stackoverflow.com/questions/37961161/how-can-show-document-file-in-iframe-when-i-get-the-file-name-from-database – Mehedi Islam Jun 22 '16 at 07:25
  • Hey Jayaraj did u see this problem what i post ? – Mehedi Islam Jun 22 '16 at 07:59