Mehedi Islam
Mehedi Islam

Reputation: 59

How can i show a document in iframe using ASP.NET MVC?

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>

Upvotes: 1

Views: 3413

Answers (1)

Jayaraj.K
Jayaraj.K

Reputation: 928

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

Upvotes: 2

Related Questions