且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当我从数据库中获取文件名时,如何在iframe中显示文档文件?

更新时间:2021-12-25 09:22:08

我发布的答案都是假设的,我对您的解决方案没有清楚的了解. 如果您不保存物理文件,请尝试将其保存在文件夹中,然后将名称保存在数据库中,或者尝试将整个路径保存在数据库中.

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.

方法1 :

如果要将文件保存在文件夹中,请说 文件 ***的方法是在web.config(位于根目录的.config文件中)中添加密钥如下

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/"/>

然后按如下所示修改您的C#代码

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");
        }

方法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.