提问人:Jonathan Small 提问时间:10/19/2023 最后编辑:marc_sJonathan Small 更新时间:10/19/2023 访问量:33
无法打开以 C#/ASP.NET MVC 应用程序生成的 PDF 文件
PDF file generated in C# / ASP.NET MVC application can not be opened
问:
我有一个 C# ASP.NET MVC 应用程序,它使用以下代码生成一个文件:.pdf
public ActionResult BuildPDF()
{
// <code executes to establish printdocs>>
PdfFileEditor oEditor = new PdfFileEditor();
oEditor.Concatenate(printdocs.ToArray(), printfinalStream);
printfinalStream.Seek(0, SeekOrigin.Begin);
return File(printfinalStream, "application/pdf", "Authorization_" + GetNewGuid() + ".pdf");
}
此代码正确生成一个文件并将其返回到浏览器,并将其放入下载文件夹中。.pdf
现在,我需要将文本字符串与文件一起返回,以便在该过程完成时可以向用户发出文本字符串警报。
这是新代码的样子:
public ActionResult BuildPDF()
{
// <code executes to establish printdocs>>
PdfFileEditor oEditor = new PdfFileEditor();
oEditor.Concatenate(printdocs.ToArray(), printfinalStream);
printfinalStream.Seek(0, SeekOrigin.Begin);
var combinedData = new CombinedData
{
Text = msg,
PdfData = printfinalStream.ToArray(), // Convert the MemoryStream to a byte array.
FileName = "Authorization_" + GetNewGuid() + ".pdf"
};
return Json(combinedData, JsonRequestBehavior.AllowGet);
}
这就是我的 Javascript 的样子:
$.ajax({
type: 'GET',
url: '@Url.Action("PrintandEmailAuthorization", "Forms")',
data: {
"LetterDated": obj.LetterDated, "AuthDated": obj.AuthDated, "ReturnBy": obj.ReturnBy, "ProtestMonth": obj.ProtestMonth,
"TaxYear": obj.TaxYear, "FromFileNum": obj.FromFileNum, "ToFileNum": obj.ToFileNum, "Suffix": obj.Suffix, "PaperSource": obj.PaperSource,
"LetterSignedBy": obj.LetterSignedBy, "Title": obj.Title, "ConsentAttorney": obj.ConsentAttorney, "includeexemption": obj.Includeexemption,
"includethirdrequest": obj.Includethirdrequest, "includeconsent": obj.Includeconsent, "includeref": obj.Includeref,
"AttorneyName": obj.AttorneyName, "AttorneyEmail": obj.AttorneyEmail, "TemplateName": obj.TemplateName, "TemplatePath": obj.TemplatePath, "Enclosures": obj.Enclosures,
"OldContingencyFee": obj.OldContingencyFee, "NewContingencyFee": obj.NewContingencyFee, "ContactID": obj.ContactID, "OptionSelected": obj.OptionSelected,
"IncludeMultLotsRe": obj.IncludeMultLotsRe, "VillTaxYear": obj.VillTaxYear, "isTest": false
},
success: function (data) {
alert(data.Text);
// Create a Blob for the PDF data.
var blob = new Blob([data.PdfData], { type: 'application/pdf' });
// Create a temporary URL for the Blob.
var url = window.URL.createObjectURL(blob);
// Create a hidden anchor element to trigger the download.
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = data.FileName;
document.body.appendChild(a);
// Simulate a click on the anchor element to trigger the download.
a.click();
// Clean up the temporary URL and anchor element.
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
});
该过程运行,我收到短信警报,文件确实下载了,但是当我尝试打开结果时,我收到一个错误,告诉我数据已损坏。.pdf
.pdf
我确定这是将数据从服务器转换到客户端的简单问题,但我不太确定。.pdf
有人有什么建议吗?
谢谢。
答: 暂无答案
评论