如何将 filename.pdf 附加到浏览器的 url 中

发布于 2024-11-27 09:49:54 字数 453 浏览 2 评论 0原文

我的控制器操作方法返回 pdf 文件

public FileContentResult GetPDF(string filename)
        {FileContentResult filecontent= new FileContentResult(Contents, "application/pdf");
               HttpContext.Response.AddHeader("Content-Disposition", "inline; filename=" + filename);
               return filecontent;
}

,因为现在,它没有在浏览器选项卡中打开,所以我想返回到 url 作为 filename.pdf 最后,这就是我如何在浏览器选项卡中打开 pdf。那么我如何将其添加到我现有的行动方法。

谢谢,
迈克尔德

My controller action method returns pdf file as

public FileContentResult GetPDF(string filename)
        {FileContentResult filecontent= new FileContentResult(Contents, "application/pdf");
               HttpContext.Response.AddHeader("Content-Disposition", "inline; filename=" + filename);
               return filecontent;
}

Now, it is not opening in a browser tab , so i want to return to url as filename.pdf at end that is how i can open pdf in browser tab.So how can i add that to my existing action method.

thanks,
michaeld

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-12-04 09:49:54

从服务器更改浏览器 URL 并同时下载文件内容是不可能的。浏览器期望从它发出请求的 URL 接收结果,因此在提供文件后您无法更改它。因此,您必须拥有以“.pdf”结尾的 url 的处理程序,并使浏览器从该 url 请求文件(已经以“.pdf”结尾)。使用 asp.net mvc,这很简单。在您的路由配置中,您现在应该有类似的内容

routes.MapRoute("PdfFile",
                "DownloadFile/GetPDF/{fileName}",
                new { controller = "DownloadFile", action = "GetPDF" });

现在,将路由更改为仅在 url 以“.pdf”结尾时匹配

routes.MapRoute("PdfFile",
                "DownloadFile/GetPDF/{fileName}.pdf",
                new { controller = "DownloadFile", action = "GetPDF" });
//note the ending .pdf at the end of the route

如果您已将系统设计为使用 ActionLink 或如果没有其他帮助者,并且没有手动编写链接,您应该已经获得了所需的结果 - 现在下载文件的链接将自动生成以 .pdf 结尾。

http 中有一些技术可以告诉浏览器(或者通常发出请求的人)它请求的资源已被移动到其他某个 url。 asp.net mvc 也支持这一点,通过返回 RedirectToAction 或类似的 actionResults。当浏览器收到重定向响应时,它会向该新 URL 发出另一个请求。在您的情况下,这可能用于将浏览器重定向到以“.pdf”结尾的网址。但这里的关键点是,即使在重定向的情况下,您当然也必须处理以“.pdf”结尾的网址:)。因此,最好通过更改路由配置来摆脱这些重定向并最初生成已指向所需 URL 的链接。

Changing the browser url from server and at the same time downloading file content is impossible. Browser expects results to be received from the url it makes request to, so you cannot change it after you serve the file. So you have to have the handler for the url ending with ".pdf", and make the browser request files from that url, already ending with ".pdf". With asp.net mvc, that is simple. In your route configuration, you should now have something similar to

routes.MapRoute("PdfFile",
                "DownloadFile/GetPDF/{fileName}",
                new { controller = "DownloadFile", action = "GetPDF" });

And now, change the route to match only when url ends with ".pdf"

routes.MapRoute("PdfFile",
                "DownloadFile/GetPDF/{fileName}.pdf",
                new { controller = "DownloadFile", action = "GetPDF" });
//note the ending .pdf at the end of the route

And if you have designed your system to generate links authomatically using ActionLink or other helpers, and not have written links by hand, you should already get the desired result - your links that download file now would automatically be generated to end with .pdf.

There's some technique in http when browser(or generally who makes the request) can be told that the resource it requests has been moved to some other url. That is supported by asp.net mvc too, by returning RedirectToAction or similar actionResults. When the browser receives redirect response, it makes another request to that new url. In your case, this could have been used to redirect browser to url ending with ".pdf". But the key point here is that even in case of redirection of course you have to handle that url ending with ".pdf" :). So its better to get rid of those redirections and initially generate links that already point to desired url, by means changing the route configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文