c# - 无法在新的 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 的文件夹。
我究竟做错了什么?
解决方案
我不得不将 ContentType 更新为“application/pdf”
return File(memStream.ToArray(), "application/pdf");
ContentType 是“应用程序/八位字节流”
推荐阅读
- algorithm - 我怎样才能找到船上最长的路?
- unique-constraint - 使用 nedb 创建羽毛服务时,是否可以定义唯一索引?
- java - 没有这样的bean例外
- mysql - 如何编辑我的 MySQL 请求最大案例输出?
- c# - 如何将 .net 包从 Visual Studio 部署到 Sharepoint 开发人员站点?
- java - 创建休眠本机查询时出现查询语法异常
- html - 滚动事件未在 Angular-6 中触发
- wpf - 将所有打开的应用程序的窗口移动到预定义的位置
- c# - 将来自多个来源的调用限制为单个方法
- macos - 终止 Vue CLI 3 开发服务器的正确方法是什么?