I want when the link being clicked first to show pdf files in new tab as preview then allow users to download the file.
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. here is my code:
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 :
$scope.onShowCertificate = function (e) {
debugger;
var id = e.key.VisaReqId;
var url = $window.location.origin + "/UploadFile/GetVisaAproveFile/?id=" + id;
$window.open(url, '_blank');
};
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. DO NOT reutrn File().
return 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.