如何在 .Net Core 中提供使用 OpenXML SDK 创建的可下载 docx 文件
How to serve a downloadable docx file created with OpenXML SDK in .Net Core
在我的 .net core 2.2 MVC 网络应用程序中,我目前正在使用 OpenXML SDK 生产力工具生成一个 .docx 文件。
目前,当用户在我的视图中填写表单并点击提交时,会触发 OpenXML 代码并生成 word 文档,并通过电子邮件发送给用户。我现在需要的是该用户也可以下载 word 文档文件。
我不确定如何重新编写我的代码才能使其正常工作。下面是我的 [HTTPPost]
方法:
[HttpPost]
public ActionResult AddReferralForm(ReferralForm referralForm)
{
try
{
//generate word document of the referral form
var GeneratedClassOpenXML = new OpenXMLGeneratedClass();
var wwwRoot = _env.WebRootPath;
string constructedFilename = DateTime.Now.ToString("yyyyMMdd") + "_"+ DateTime.Now.ToString("HHmm") + "_";
string fileName = referralFormsLocation + constructedFilename+".docx";
var emailaddressToSendForm = _context.ReferralFormEmailAddresses.FirstOrDefault();
//Generate Word Document referral form - uses Open XML SDK
GeneratedClassOpenXML.CreatePackage(fileName, referralForm, LoggedInUser, emailaddressToSendForm.EmailAddress);
//method for user to download the word doc
ExportReferral(fileName);
return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id });
}
catch (Exception ex)
{
throw new Exception("Error when saving a referral - " + ex);
}
}
此方法将用户重定向回 Home/Index
。生成的word文档发邮件在GeneratedClassOpenXML.CreatePackage()
.
内
我认为类似这段代码的东西可以工作,上面的 HTTPPost 方法调用它,但这没有工作:
[HttpGet]
public IActionResult ExportReferral(string filePath)
{
var x = System.IO.File.ReadAllBytes(filePath);
return File(System.IO.File.ReadAllBytes(filePath),
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileDownloadName: "MySheet.docx");
}
我正在寻找一种方法 return 用户到主页和 return 文件内容。有什么办法可以做到这一点?
谢谢
使用路径重定向到主页
[HttpPost]
public ActionResult AddReferralForm(ReferralForm referralForm)
{
//Generate Word Document referral form - uses Open XML SDK
...
redirect home/index with fileName
return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id, file= fileName });
}
Index.cshtml
<!-- you can have your own style -->
<style>
/* dialog div must be hidden */
#basicModal {
display: none;
}
</style>
<div id="basicModal">
Confirm to download ?
</div>
<script>
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const downloadUrl = urlParams.get('file');
//window.location = downloadUrl;
if (downloadUrl != null) {
$("#basicModal").dialog({
modal: true,
title: "Are you sure?",
buttons: {
"YES": function () {
window.open(downloadUrl, '_blank');
},
"NO": function () {
$(this).dialog("close");
}
}
});
}
});
</script>
在我的 .net core 2.2 MVC 网络应用程序中,我目前正在使用 OpenXML SDK 生产力工具生成一个 .docx 文件。
目前,当用户在我的视图中填写表单并点击提交时,会触发 OpenXML 代码并生成 word 文档,并通过电子邮件发送给用户。我现在需要的是该用户也可以下载 word 文档文件。
我不确定如何重新编写我的代码才能使其正常工作。下面是我的 [HTTPPost]
方法:
[HttpPost]
public ActionResult AddReferralForm(ReferralForm referralForm)
{
try
{
//generate word document of the referral form
var GeneratedClassOpenXML = new OpenXMLGeneratedClass();
var wwwRoot = _env.WebRootPath;
string constructedFilename = DateTime.Now.ToString("yyyyMMdd") + "_"+ DateTime.Now.ToString("HHmm") + "_";
string fileName = referralFormsLocation + constructedFilename+".docx";
var emailaddressToSendForm = _context.ReferralFormEmailAddresses.FirstOrDefault();
//Generate Word Document referral form - uses Open XML SDK
GeneratedClassOpenXML.CreatePackage(fileName, referralForm, LoggedInUser, emailaddressToSendForm.EmailAddress);
//method for user to download the word doc
ExportReferral(fileName);
return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id });
}
catch (Exception ex)
{
throw new Exception("Error when saving a referral - " + ex);
}
}
此方法将用户重定向回 Home/Index
。生成的word文档发邮件在GeneratedClassOpenXML.CreatePackage()
.
我认为类似这段代码的东西可以工作,上面的 HTTPPost 方法调用它,但这没有工作:
[HttpGet]
public IActionResult ExportReferral(string filePath)
{
var x = System.IO.File.ReadAllBytes(filePath);
return File(System.IO.File.ReadAllBytes(filePath),
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileDownloadName: "MySheet.docx");
}
我正在寻找一种方法 return 用户到主页和 return 文件内容。有什么办法可以做到这一点?
谢谢
使用路径重定向到主页
[HttpPost]
public ActionResult AddReferralForm(ReferralForm referralForm)
{
//Generate Word Document referral form - uses Open XML SDK
...
redirect home/index with fileName
return RedirectToAction("Index", "Home", new { id = LoggedInUser.Id, file= fileName });
}
Index.cshtml
<!-- you can have your own style -->
<style>
/* dialog div must be hidden */
#basicModal {
display: none;
}
</style>
<div id="basicModal">
Confirm to download ?
</div>
<script>
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const downloadUrl = urlParams.get('file');
//window.location = downloadUrl;
if (downloadUrl != null) {
$("#basicModal").dialog({
modal: true,
title: "Are you sure?",
buttons: {
"YES": function () {
window.open(downloadUrl, '_blank');
},
"NO": function () {
$(this).dialog("close");
}
}
});
}
});
</script>