我花了一整天的时间试图强制浏览器打开 PDF 文件,而不是使用System.Web.Mvc.FileResult和下载它,我以旧方式 System.Web.Response 这样做,如下所示:控制器 //StaticData.ReportsTemp it's my type of Dictionary<string,object>public async Task<ActionResult> GenerateReport() { //some code var tempGuid = Guid.NewGuid().ToString(); StaticData.ReportsTemp[tempGuid] = File(strem, obj.MediaType, obj.name); return Content(opertionGuid);}public FileResult DownloadReport(string fileName) { FileResult result = null; FileStreamResult st = null; if (StaticData.ReportsTemp.ContainsKey(fileName)) { st = StaticData.ReportsTemp[fileName] as FileStreamResult; result = st; StaticData.ReportsTemp.Remove(fileName); if (st.ContentType == System.Net.Mime.MediaTypeNames.Application.Pdf || 1 == 1) { //Old fashion way byte[] buff = null; System.IO.BinaryReader br = new System.IO.BinaryReader(st.FileStream); buff = br.ReadBytes((int) st.FileStream.Length); Response.BinaryWrite(buff); Response.AddHeader("content-length", buff.Length.ToString()); Response.ContentType = st.ContentType; Response.AddHeader("Content-Disposition", "inline; filename=" + st.FileDownloadName + ";"); return null; } } return result;}JSfunction onSuccess(data, status, xhr) { window.open("@Url.RouteUrl(new { Controller = "Main", Action = "DownloadReport" })?filename=" + xhr.responseText,'_brank');}我的下载过程分为两个步骤:1)我正在生成报告,创建 FileStremResult ,保存到 StaticData.Reports 字典中并将字典键返回给客户端2) 使用从步骤 1 中收到的字典键,我正在调用从 StaticData.Reports 字典返回 FileStremResult 的操作下载PDF 在浏览器中打开,所有其他文件仍然只是下载。但我仍然想知道是否有其他方法可以实现这一目标?有没有办法用 System.Web.Mvc ActionResult 的类来做到这一点?
1 回答
- 1 回答
- 0 关注
- 263 浏览
添加回答
举报
0/150
提交
取消