有没有办法在浏览器中使用具有特定名称的ASP.NET MVC FileContentResult来流式传输文件?
我注意到你可以有一个FileDialog(打开/保存),或者你可以在浏览器窗口中流式传输文件,但是当你尝试保存文件时它会使用ActionName.
我有以下场景:
byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote);
result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId));
Run Code Online (Sandbox Code Playgroud)
当我使用它时,我可以流式传输字节,但是给用户提供了一个OPEN/SAVE文件对话框.我想在浏览器窗口中实际传输此文件.
如果我只使用FilePathResult,它会在浏览器窗口中显示该文件,但是当我单击"保存"按钮将文件保存为PDF时,它会显示操作名称作为文件的名称.
有没有遇到过这个?
这可能是一个简单的,但这里是:
我正在我的MVC3应用程序中实现一个excel可下载的报告.我过去曾使用过这种方法而且效果很好,但在这种情况下,报告中可能不存在销售数据.这是我的代码:
我在Reports控制器中有一个FileResult操作:
[HttpPost]
public FileResult ExcelReportDownload(ReportExcelDownloadRequest reportRequest)
{
ReportEngine re = new ReportEngine();
Stream report = re.GetReport(reportRequest);
return new FileStreamResult(report, "application/ms-excel")
{
FileDownloadName = "SalesReport.xls"
};
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有时报告流可能为空,这意味着没有可用的销售信息,在这种情况下,我宁愿重定向到显示消息的视图,说没有可用的销售信息,但是我不知道如何实现这个.
有没有办法做到这一点?
用户完成下载后我想做点什么
public class TestController : Controller
{
public FilePathResult Index()
{
return File("path/to/file", "mime");
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试的是将以下事件添加到测试控制器,但它们都在用户完成下载之前触发(除了析构函数,它永远不会被调用)
protected override void EndExecute(IAsyncResult asyncResult)
{
base.EndExecute(asyncResult);
}
protected override void EndExecuteCore(IAsyncResult asyncResult)
{
base.EndExecuteCore(asyncResult);
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
~TestController()
{
//
}
Run Code Online (Sandbox Code Playgroud) 我的asp.net-mvc项目中有以下javascript代码和控制器操作:
使用Javascript:
$("#exportPPT").live('click', function (e) {
window.location.href = "/Initiative/GenerateFile" + GenerateParams();
});
Run Code Online (Sandbox Code Playgroud)
C#控制器:
public ActionResult GenerateFile(MyParams myParams)
{
var template = Server.MapPath(PPT_ROOT + "/template.pptx");
IEnumerable<Order> orders = Model.GetOrders(myparams);
var pptResults = GeneratePowerpointFile(orders);
return File(pptResults.Content, "application/vnd.ms-powerpoint", pptResults.FileName);
}
Run Code Online (Sandbox Code Playgroud)
但是在某些情况下,让我们说当orders.Count()为0然后而不是生成文件时,我宁愿向用户回复一条错误信息,说明你有错误.
鉴于上述代码,实现此目的的最佳方法是什么?我想把它改成ajax调用,但我不知道如何在json请求中下载我的Fie()和包(如果支持的话).
有什么建议?
我在asp.net mvc4中有一个fileresult方法,它在excel文件中返回一个报告.现在,如果我的条件不满足,我怎么能从这个方法返回错误信息!! 既然我们只能从这个方法返回一个文件?!Thnks
是否有可能byte[]
从FileResult
C#获得?
我有一个压缩文件,我试图流到客户端:
public FileResult GetFiles()
{
return File("test.zip", "application/zip");
}
Run Code Online (Sandbox Code Playgroud)
文件下载但没有".zip"扩展名.
希望以下代码是使用ASP.NET MVC 3返回磁盘上存在的映像的正确方法:
public FilePathResult GetThumbnail(string imageName)
{
if( !String.IsNullOrEmpty(imageName) &&
Regex.IsMatch(imageName, @"^p\d{10}.jpg$"))) ) // p0000000000.jpg
{
var homePath = Server.MapPath("~/Content/previews");
var imagePath = Path.Combine( homePath, imageName );
if( System.IO.File.Exists(imagePath) )
return this.File(imagePath, "image/jpeg");
}
return ???
}
Run Code Online (Sandbox Code Playgroud)
如果您没有找到该文件,您可以返回哪些代表HTML 404错误(或等效的?)
如果文件名包含重音符号,则它在Opera,FF,Chrome和IE9中按预期工作.
但在IE8文件类型中是"未知文件类型",并显示"文件"作为文件名(实际上是URL的最后一部分).
有没有人知道解决方法?替换文件名中的"特殊"字符除外?
测试代码:(文件|新项目|添加控制器)
public class FileController : Controller
{
public ActionResult Index(bool? Accents)
{
byte[] content = new byte[] { 1, 2, 3, 4 };
return File(content, "application/octet-stream", true.Equals(Accents) ? "dsaé.txt" : "dsae.txt");
}
}
Run Code Online (Sandbox Code Playgroud)
像这样测试: http:// localhost/file, http:// localhost/file?accents = true
public class FileContentResultStupidIE : FileContentResult //yeah, maybe i am not totally "politically correct", but still...
{
public FileContentResultStupidIE(byte[] fileContents, string contentType) : base(fileContents, contentType) { }
public override void ExecuteResult(ControllerContext context)
{
var b = …
Run Code Online (Sandbox Code Playgroud) 我试图通过控制器ActionResult返回大文件,并实现了如下自定义FileResult类.
public class StreamedFileResult : FileResult
{
private string _FilePath;
public StreamedFileResult(string filePath, string contentType)
: base(contentType)
{
_FilePath = filePath;
}
protected override void WriteFile(System.Web.HttpResponseBase response)
{
using (FileStream fs = new FileStream(_FilePath, FileMode.Open, FileAccess.Read))
{
int bufferLength = 65536;
byte[] buffer = new byte[bufferLength];
int bytesRead = 0;
while (true)
{
bytesRead = fs.Read(buffer, 0, bufferLength);
if (bytesRead == 0)
{
break;
}
response.OutputStream.Write(buffer, 0, bytesRead);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我遇到的问题是整个文件似乎被缓冲到内存中.我需要做些什么来防止这种情况发生?
fileresult ×10
asp.net-mvc ×8
c# ×5
bytearray ×1
controller ×1
diacritics ×1
jquery ×1
powerpoint ×1
streaming ×1