首页 > 解决方案 > 无法在新的 Chrome 浏览器选项卡中打开 pdf

问题描述

我不知道如何在新的 Chrome 选项卡中打开 pdf。以下代码适用于 Internet Explorer,但不适用于 Chrome。我检查了 Chrome 中的设置,它设置为以下内容:

在此处输入图像描述

我的观点:

@Html.ActionLink("View", "OpenFileEncrypted", "Storage", new { fileNumber = item.accession_number, fileType = 2, openType = "NewTab" }, new { @class = "btn btn-primary ViewDownloadButton", @runat="server", @target = "_blank", @onclick = "javascript:clickView(" + item.accession_number + ")" })

javascript 函数 ClickView 检查网页上的复选框。

我的控制器:

 public ActionResult OpenFileEncrypted(int? fileNumber, int? fileType, string openType)
    {            
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(      
      CloudConfigurationManager.GetSetting("StorageConnectionString"));

      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

      CloudBlobContainer container = GetContainer(blobClient, Convert.ToInt32(fileType));

      KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(GetToken);

      var fileName = Convert.ToString(fileNumber) + ".pdf";
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

      BlobEncryptionPolicy policy = new BlobEncryptionPolicy(null, cloudResolver);
      BlobRequestOptions options = new BlobRequestOptions() { EncryptionPolicy = policy };

      var memStream = new MemoryStream();
      blockBlob.DownloadToStream(memStream, null, options, null);

      if (openType == null)
      {
       openType = "NewTab";
      }

      order_info OInfo = Ldb.order_info.Where(o => o.accession_number == fileNumber).FirstOrDefault();

       switch (openType)
       {
         case "NewTab":
             Response.AddHeader("Content-Disposition", "Inline; filename=" + fileName);
             OInfo.ReportViewed = true;
             break;
         case "Download":
             Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
             OInfo.ReportDownloaded = true;
             break;
       }

       if (OInfo != null)
       {
          Ldb.Entry(OInfo).State = EntityState.Modified;
          Ldb.SaveChanges();
       }

       return File(memStream.ToArray(), blockBlob.Properties.ContentType);            

它适用于 IE,但不适用于 Chrome。单击该按钮时,它会打开一个新选项卡,然后打开一个下载框以命名并选择下载 .pdf 的文件夹。

我究竟做错了什么?

标签: c#asp.net-mvcgoogle-chromepdftabs

解决方案


我不得不将 ContentType 更新为“application/pdf”

return File(memStream.ToArray(), "application/pdf");

ContentType 是“应用程序/八位字节流”


推荐阅读