c# - .NET MVC:在服务器上执行繁重的任务,然后在完成时向用户发送电子邮件,以便用户可以离开站点而不是等待?
问题描述
场景:一个按钮允许用户合并大量 PDF 文档以下载为单个 PDF。目前,在用户必须等待下载开始时,获取所有 PDF 和合并的操作可能需要一分钟或更长时间。
我的目标是让用户愿意离开。我想到的解决方案是在服务器的后台合并文档,然后在完成后通过电子邮件将链接发送给用户,但我对其他解决方案持开放态度。
我不明白的是如何在后台异步执行合并。使用 .NET、MVC 5、DevExpress。
代码看起来有点像:
$.ajax({
type: "POST",
url: '@Url.Action("ExportMergedDocuments_PersonnelId", "Personnel", new { personnelId = Model.Id })',
}).done(function(data) {
window.location.href = '@Url.RouteUrl(new { Controller = "Personnel", Action = "Download"})/?file=' + data.fileName; }
});
[HttpPost]
public JsonResult ExportMergedDocuments_PersonnelId(int PersonnelId)
{
var allDocuments = new DataSet();
allDocuments.Merge(GetDocuments((int)PersonnelId, ".....1").Tables[0]);
allDocuments.Merge(GetDocuments((int)PersonnelId, ".....2").Tables[0]);
string fileName = $"merged__{DateTime.Now.ToString("yyyyMMddHHmm")}.pdf";
if (MergePdfSet(fileName, allDocuments))
return Json(new { fileName });
// else error msg
}
下载文件:
[HttpGet]
public ActionResult Download(string file)
{
return File(..fullpath.., "application/pdf", file);
}
合并PDF:
public bool MergePdfSet(string fileName, DataSet allDocuments)
{
bool merged = false;
string fullPath = Path.Combine(Server.MapPath("~/App_Data/temp/"), fileName);
using (var pdfDocumentProcessor = new PdfDocumentProcessor())
{
pdfDocumentProcessor.CreateEmptyDocument(fullPath);
foreach (DataRow row in allDocuments.Tables[0].Rows)
{
var documentId = (int)row["DocumentID"];
var fetchedDocument = GetFile(documentId);
pdfDocumentProcessor.AppendDocument(fetchedDocument);
merged = true;
}
}
return merged;
}
解决方案
想到了两个选择:
- 创建一个新线程并在那里运行代码,但不要等待它。
- 使用 Hangfire ( https://www.hangfire.io/ ),您可以轻松入队工作。
推荐阅读
- java - 如何在Java中将字符串转换为数值
- javascript - 访问在动态导入的 JavaScript 中声明的变量?
- laravel - 使用 https://github.com/Maatwebsite/Laravel-Excel 调用成员函数 has() on null 时出错
- asp.net-core - 在 asp .net core 中写入一个 excel 文件
- amazon-web-services - 用户在注册时未收到确认码以及如何在 aws-amplify-react-native 中为此案例启用 MFA
- react-native - 错误命令失败:gradlew.bat app:installDebug
- python - 对熊猫中字符串的一部分进行分组
- kotlin - 使用文件模板生成 Kotlin 代码
- awk - 无法在此脚本中将文件复制到 linux 中的用户
- excel - 脚本任务在 Visual Studio 中工作,但在部署包时不工作