在带有名称的浏览器中使用 ASP.NET MVC FileContentResult 流式传输文件? [英] Stream file using ASP.NET MVC FileContentResult in a browser with a name?

查看:21
本文介绍了在带有名称的浏览器中使用 ASP.NET MVC FileContentResult 流式传输文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在具有特定名称的浏览器中使用 ASP.NET MVC FileContentResult 流式传输文件?

我注意到您可以使用 FileDialog(打开/保存),也可以在浏览器窗口中流式传输文件,但是当您尝试保存文件时它将使用 ActionName.

我有以下场景:

byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote);result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId));

当我使用它时,我可以流式传输字节,但是会向用户提供一个打开/保存文件对话框.我想在浏览器窗口中实际流式传输此文件.

如果我只使用 FilePathResult,它会在浏览器窗口中显示文件,但是当我单击保存"按钮将文件保存为 PDF 时,它会向我显示操作名称作为文件名.

有人遇到过这种情况吗?

解决方案

这可能对遇到此问题的其他人有所帮助.我终于想出了一个解决方案.事实证明,即使我们为content-disposition"使用内联并指定文件名,浏览器仍然不使用文件名.相反,浏览器会尝试根据路径/URL 解释文件名.

您可以在此 URL 上进一步阅读:使用正确的文件名在浏览器中安全下载文件

这给了我一个想法,我刚刚创建了我的 URL 路由,它将转换 URL 并以我想要提供文件的文件名结尾.所以例如我原来的控制器调用只包括传递正在打印的订单的订单 ID.我期望文件名的格式为 Order{0}.pdf,其中 {0} 是订单 ID.同样,对于引号,我想要 Quote{0}.pdf.

在我的控制器中,我只是继续添加了一个额外的参数来接受文件名.我在 URL.Action 方法中将文件名作为参数传递.

然后我创建了一个新的路由,将该 URL 映射到以下格式:http://localhost/ShoppingCart/PrintQuote/1054/Quote1054.pdf

<前><代码>route.MapRoute("", "{controller}/{action}/{orderId}/{fileName}",新 { 控制器 = "ShoppingCart", action = "PrintQuote" }, 新字符串[] { "x.x.x.Controllers" });

这几乎解决了我的问题.希望这对某人有所帮助!

干杯,安普

Is there a way to stream a file using ASP.NET MVC FileContentResult within the browser with a specific name?

I have noticed that you can either have a FileDialog (Open/Save) or you can stream the file in a browser window, but then it will use the ActionName when you try to save the file.

I have the following scenario:

byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote);
result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId));

When I use this, I can stream the bytes, but a OPEN/SAVE file dialog is given to the user. I would like to actually stream this file in a browser window.

If I just use the FilePathResult, it shows the file in a browser window, but then when I click on "Save" button to save the file in PDF, it shows me the Action Name as the name of the file.

Has anyone encountered this?

解决方案

This might be helpful for whoever else faces this problem. I finally figured out a solution. Turns out, even if we use the inline for "content-disposition" and specify a file name, the browsers still do not use the file name. Instead browsers try and interpret the file name based on the Path/URL.

You can read further on this URL: Securly download file inside browser with correct filename

This gave me an idea, I just created my URL route that would convert the URL and end it with the name of the file I wanted to give the file. So for e.g. my original controller call just consisted of passing the Order Id of the Order being printed. I was expecting the file name to be of the format Order{0}.pdf where {0} is the Order Id. Similarly for quotes, I wanted Quote{0}.pdf.

In my controller, I just went ahead and added an additional parameter to accept the file name. I passed the filename as a parameter in the URL.Action method.

I then created a new route that would map that URL to the format: http://localhost/ShoppingCart/PrintQuote/1054/Quote1054.pdf


routes.MapRoute("", "{controller}/{action}/{orderId}/{fileName}",
                new { controller = "ShoppingCart", action = "PrintQuote" }
                , new string[] { "x.x.x.Controllers" }
            );

This pretty much solved my issue. Hoping this helps someone!

Cheerz, Anup

这篇关于在带有名称的浏览器中使用 ASP.NET MVC FileContentResult 流式传输文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆