无法在新的Chrome浏览器标签中打开pdf

时间:2018-05-22 16:41:05

标签: c# asp.net-mvc google-chrome pdf tabs

我无法弄清楚如何在新的Chrome标签页中打开PDF文件。以下代码适用于Internet Explorer,但不适用于Chrome。我在Chrome中检查了设置,并将其设置为以下内容:

enter image description here

我的观点:

@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检查Web上的复选框 页。

我的控制器:

 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的文件夹。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我必须将ContentType的更新更新为“application / pdf”

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

ContentType为“application / octet-stream”