Skip to content Skip to sidebar Skip to footer

How Can Show Document File In Iframe When I Get The File Name From Database?

How can show document file in iframe when i get the file name from database?My database table name is File and my viewmodel is MyFileModel. public ActionResult Create(HttpPos

Solution 1:

The answer which I am posting is all on an assumption, I do not have clear picture of your solution. If you are not saving a physical file try to save it in a folder and then save the name in the DB OR try to save the entire path in the DB.

Method 1 :

If you are saving the file in a folder say File best way is to add a key in the web.config(.config file which is at the root) as follows

<add key="FilePath"value= "~/File/"/>

and then modify your C# code as follows

public FileStreamResult GetPDF()
        {
            var file = db.Files.Single(s => s.Id == 1);
            string fileName = file.FileName;
            string filePath = ConfigurationManager.AppSettings["FilePath"] + fileName;
            FileStream fs = new FileStream(Server.MapPath(filePath), FileMode.Open, FileAccess.Read);
            return File(fs,"text/plain"); // "text/plain" if your file content-type is text file//return View(Server.MapPath("~/File/SegmentAdd.txt"));//return File(fs,"text/plain");
        }

Method 2 :

If you are saving the entire path then it makes coding much more simpler and you need not change the code you have written, just go ahead with the same.

Hope this would help you.

Solution 2:

I think the way the pdf will open is dependent on the browser that the user uses.

You probably need something like: https://pdfobject.com/.

Good luck!

Post a Comment for "How Can Show Document File In Iframe When I Get The File Name From Database?"