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>
4
  • 1
    This depends on the browser settings and the server http headers. See stackoverflow.com/questions/6293893/… for more details. Commented Jun 21, 2016 at 5:16
  • i don't understand now what should i do?if i use like that... return File(fs,"application/pdf","SegmentAdd.txt"); Commented Jun 21, 2016 at 5: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. Commented Jun 21, 2016 at 5: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...? Commented Jun 21, 2016 at 6:15

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you,Jayaraj.K It works. But one question when file type like .pdf/.doc or etc. what should be the content type?
It depend upon your file which you are trying to show. Please refer freeformatter.com/mime-types-list.html, MIME Type column you can find content type to be used for each file.
Thank you very much Jayaraj.K
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?
What problem you are facing in MapPath. Does it throws any error, If so post the issue as another question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.