簡體   English   中英

MVC 5如何強制瀏覽器打開PDF文件

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

我花了一整天的時間試圖迫使瀏覽器打開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;
}

JS

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

我的下載過程分為兩個步驟:

  1. 1)我生成報告,創建FileStremResult,保存到StaticData.Reports字典中,並將字典密鑰返回給客戶端
  2. 2)使用從步驟1中獲得的字典鍵,我正在調用操作Download,該操作從StaticData.Reports字典返回FileStremResult

在瀏覽器中打開了PDF,所有其他文件仍然只是下載而已。

但是我仍然想知道是否還有其他方法可以實現這一目標? 有什么辦法用System.Web.Mvc ActionResult的類來做到這一點?

我不知道StaticData類的工作方式。 通常,您可以返回FileResult ,並且如果文件無效或不存在,則可以顯示PageNotFound頁面。

注意:如果客戶端未安裝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