如何将 filename.pdf 附加到浏览器的 url 中
我的控制器操作方法返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从服务器更改浏览器 URL 并同时下载文件内容是不可能的。浏览器期望从它发出请求的 URL 接收结果,因此在提供文件后您无法更改它。因此,您必须拥有以“.pdf”结尾的 url 的处理程序,并使浏览器从该 url 请求文件(已经以“.pdf”结尾)。使用 asp.net mvc,这很简单。在您的路由配置中,您现在应该有类似的内容
现在,将路由更改为仅在 url 以“.pdf”结尾时匹配
如果您已将系统设计为使用
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
And now, change the route to match only when url ends with ".pdf"
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.