简体   繁体   English

包含pdf文件的页面显示5分钟后,重定向到另一页面

[英]Redirect to another page after a page with a pdf file displays for 5 minutes

please I'm having a hard time getting this to work. 请让我很难工作。 I'm fairly new to MVC. 我是MVC的新手。

So here is my challenge. 所以这是我的挑战。 I have an actionResult in my controller that reads a pdf file and displays the pdf in a browser:` 我的控制器中有一个actionResult,可以读取pdf文件并在浏览器中显示pdf:

public ActionResult ViewPdf(DocumentSearchInputModel searchInputModel)
        {
            var realPath =
                     "C:/Users/ionyejekwe/Documents/Visual Studio 2015/Projects/OnlineSearchLagos/OnlineSearchLagos/assets/img/gtb.pdf";
            var fileStream = new FileStream(realPath, FileMode.Open, FileAccess.Read);
            var fsResult = new FileStreamResult(fileStream, "application/pdf");
            return File(fsResult, "application/pdf");
        }`

In a view called searchResult.cshtm, I have this <a href="@Url.Action("ViewPdf", "Search", Model)" class="btn btn-xs btn-danger">View pdf</a> 在名为searchResult.cshtm的视图中,我有这个<a href="@Url.Action("ViewPdf", "Search", Model)" class="btn btn-xs btn-danger">View pdf</a>

which is supposed to call the pdf actionResult and render it to the browser. 应该调用pdf actionResult并将其呈现给浏览器。 It works perfectly fine. 它工作得很好。 But I want to close the pdf page, after 5 minutes. 但是我想在5分钟后关闭pdf页面。 I have tried many ways, but none seems to be working. 我尝试了很多方法,但是似乎都没有用。 I tried using this ajax:` 我尝试使用此ajax:

<script>
    $.ajax({
        url: '@Url.Action("ViewPdf", "Search")',
        contentType: "application/pdf; charset=utf-8",
        success: function() { alert('Success'); },
        error: function () { alert('A error'); }
    });

</script>`

just to see if the ajax will get to the page, but the ajax code its not even getting to the pdf page, it's been triggered in the page where the pdf is being called. 只是为了查看ajax是否会到达页面,但是ajax代码甚至没有到达pdf页面,它是在调用pdf的页面中触发的。 Please how do i get the pdf page to close or redirect to another page after 5 minutes. 请在5分钟后如何获取pdf页面以关闭或重定向到另一页面。 I will really appreciate any help. 我将非常感谢您的帮助。

Maybe try it with the window.open() function. 也许用window.open()函数尝试一下。 It opens a new tab with the first parameter as its URL. 它将打开一个新选项卡,其中第一个参数为URL。 I have no experience with ASP so I am not trying to get the URL of your PDF. 我没有使用ASP的经验,所以我没有尝试获取您的PDF的URL。

var newWindow = window.open("your_url_here");
setTimeout(function(){newWindow.close()}, 300000);

what this will do is open a new tab with the PDF and close it after 300000ms = 5min. 要做的是打开一个带有PDF的新标签,并在300000ms = 5min之后关闭它。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM