简体   繁体   English

MVC 5如何强制浏览器打开PDF文件

[英]MVC 5 how to force browser to open PDF file

I spend the whole day trying to force a browser to open PDF files instead of download it using System.Web.Mvc.FileResult and in the and I did it in old fashion way System.Web.Response like this: 我花了一整天的时间试图迫使浏览器打开PDF文件,而不是使用System.Web.Mvc.FileResult下载它,并且在中,我以老式的方式System.Web.Response进行了如下操作:

Contoller 位指示

 //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;
}

JS JS

function onSuccess(data, status, xhr) {
    window.open("@Url.RouteUrl(new { Controller = "Main", Action = "DownloadReport" })?filename=" + xhr.responseText,'_brank');
}

My downloading process is divided on two steps: 我的下载过程分为两个步骤:

  1. 1) I'm generation report , create FileStremResult ,saving into StaticData.Reports dictionary and return dictionary key to client 1)我生成报告,创建FileStremResult,保存到StaticData.Reports字典中,并将字典密钥返回给客户端
  2. 2) using dictionary key recived from step 1 I'm calling action Download which return FileStremResult from StaticData.Reports dictionary 2)使用从步骤1中获得的字典键,我正在调用操作Download,该操作从StaticData.Reports字典返回FileStremResult

PDF are opened in browser and all other files still are simply just downloading. 在浏览器中打开了PDF,所有其他文件仍然只是下载而已。

But I'm still wondering if there is any other way to achieve this? 但是我仍然想知道是否还有其他方法可以实现这一目标? Is there any way to do this with System.Web.Mvc ActionResult's classes ? 有什么办法用System.Web.Mvc ActionResult的类来做到这一点?

I do not know how StaticData class work. 我不知道StaticData类的工作方式。 Normally, you could return FileResult , and if file is not valid or doesn't exist, you could display PageNotFound page. 通常,您可以返回FileResult ,并且如果文件无效或不存在,则可以显示PageNotFound页面。

Note: if client doesn't have Acrobat installed, you cannot force them to view inside browser. 注意:如果客户端未安装Acrobat,则无法强制他们在浏览器中查看。

public class ReportFile
{
    public byte[] Data { get; set; }
    public string FileName { get; set; }
    public string ContentType { get; set; }
}

public async Task<ActionResult> GenerateReport()
{
    //some code 
    var tempGuid = Guid.NewGuid().ToString();
    StaticData.ReportsTemp[tempGuid] = new ReportFile
    {
        Data = data, // Convert stream to byte array
        ContentType = obj.MediaType,
        FileName = obj.name
    };
    return Content(tempGuid);
}

public ActionResult DownloadReport(string fileName)
{
    if (StaticData.ReportsTemp.ContainsKey(fileName))
    {
        ReportFile file = StaticData.ReportsTemp[fileName];
        StaticData.ReportsTemp.Remove(fileName);

        if (file.ContentType == MediaTypeNames.Application.Pdf)
        {
            var cd = new ContentDisposition { FileName = file.FileName, Inline = true };
            Response.AppendHeader("Content-Disposition", cd.ToString());
            return File(file.Data, file.ContentType);
        }
    }
    return View("PageNotFound");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM