简体   繁体   English

"Content-Disposition", "inline;filename=" + filName 不强制显示 pdf 文件

[英]"Content-Disposition", "inline;filename=" + filName doesnt force pdf files to show

I want when the link being clicked first to show pdf files in new tab as preview then allow users to download the file.我希望首先单击链接以在新选项卡中显示 pdf 文件作为预览,然后允许用户下载文件。

so i looked for the answer and they mostly say that i should include these two headers所以我寻找答案,他们大多说我应该包括这两个标题

Response.AddHeader("Content-Disposition", "inline; filename=\"" + filName + "\"");
Response.AddHeader("Content-Type", "application/pdf");

but it doesnt work and pdf files just start to download.但它不起作用,pdf文件才开始下载。 here is my code:这是我的代码:

C# : C# :

public FileContentResult GetVisaAproveFile(Guid id)
    {
        var request = visaRequestRepository.GetById(id);
        AttachedFile ApprovedAttachedFile = request.AttachedFiles.FirstOrDefault(f => f.DocType == DocType.ApprovedDocument);
        byte[] filByte = ApprovedAttachedFile?.FileContent ?? new byte [0];
        string filName = ApprovedAttachedFile?.AttachFileSource ?? "بدون عنوان";
        Response.Clear();
        Response.AddHeader("Content-Type", "application/pdf");
        Response.AddHeader("Content-Disposition", "inline; filename=\"" + filName + "\"");
        return File(filByte, MimeMapping.GetMimeMapping(filName));
    }

JavaScript : JavaScript :

$scope.onShowCertificate = function (e) {
        debugger;
        var id = e.key.VisaReqId;

        var url = $window.location.origin + "/UploadFile/GetVisaAproveFile/?id=" + id;
        $window.open(url, '_blank');

    };

HTML : HTML :

<div data-options="dxTemplate:{ name:'Certificate' }">
                            <i class='glyphicon glyphicon-download-alt ajaxresub'
                               ng-if="item.value == 8"
                               style='color: blue;cursor: pointer !important;'
                               ng-click="onShowCertificate(item)"></i>
 </div>

thanks in advance :)提前致谢 :)

here is my example :这是我的例子:

use FileResult in your action.在您的操作中使用FileResult DO NOT reutrn File().不要返回 File()。

return FileStreamResult() .返回FileStreamResult()

[HttpGet]
public FileResult GetVisaAproveFile(string id)
{
  //skip content
  //Put your PDF in MemoryStream 
  MemoryStream msResult = ReadPDF();

  var fileResult = new FileStreamResult(msResult, "application/pdf");
  Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}.pdf", id));

  return fileResult;
}

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

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