asp.net-core-mvc - 访问 wwwroot - Asp.Net Core MVC 在本地主机上运行良好,但在已发布的应用程序中运行良好
问题描述
我在尝试让我的应用程序在发布时工作时遇到了很多麻烦。基本上,代码应该使用 Open XML sdk 从模板创建一个文档,然后保存到 wwwroot,然后上传到 blob 存储。
使用本地主机工作正常。已经阅读并尝试了一些重新访问静态文件的东西 - 但似乎没有任何效果。任何帮助将不胜感激。相关代码如下:
[HttpGet]
public IActionResult GenerateDocxBrowser(MemoryStream mem, string filepath, string inputLastName, string inputTitle, string requestID, string dateReceived, string complaintType, string complaintDetails, string nameString, string no, string street, string town, string postcode)
{
var list = _context.Complaints.Where(s => s.ComplaintId.ToString().Contains(requestID)).ToList();
using (mem = new MemoryStream())
{
filepath = @"wwwroot\RequestTemplate.docx";
nameString = list.Select(s => s.NameString).FirstOrDefault();
complaintDetails = list.Select(s => s.Complaint).FirstOrDefault();
street = list.Select(s => s.AddressStreet).FirstOrDefault();
town = list.Select(s => s.AddressTown).FirstOrDefault();
using (WordprocessingDocument document = WordprocessingDocument.Open(filepath,true))
{
document.GetMergeFields("LastName").ReplaceWithText(inputLastName);
document.GetMergeFields("Title").ReplaceWithText(inputTitle);
document.GetMergeFields("ComplaintID").ReplaceWithText(requestID);
document.GetMergeFields("DateReceived").ReplaceWithText(dateReceived);
document.GetMergeFields("ComplaintType").ReplaceWithText(complaintType);
document.GetMergeFields("ComplaintDetails").ReplaceWithText(complaintDetails);
document.GetMergeFields("NameString").ReplaceWithText(nameString);
document.GetMergeFields("AddressLn1").ReplaceWithText(no + " " + street);
document.GetMergeFields("AddressLn2").ReplaceWithText(town + " TAS " + postcode);
document.SaveAs(@"wwwroot\" + requestID + ".docx");
document.MainDocumentPart.Document.Save();
document.Close();
}
}
const string StorageAccountName = "xxx";
const string StorageAccountKey = "xxxxxx";
var storageAccount = new CloudStorageAccount(
new StorageCredentials(StorageAccountName, StorageAccountKey), true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("tasman/Request Images");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(requestID + ".docx");
blockBlob.UploadFromFileAsync(@"wwwroot\" + requestID + ".docx");
return View();
}
解决方案
您的.SaveAs()
字段应该是相对的,目前它实际上保存到wwwroot
驱动器上的某个位置。您可以通过几种不同的方式指定相对路径 - 其中之一如下:
var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");
推荐阅读
- r - 如何在 R plotly 图中为每条轨迹定义单独的调色板?
- reactjs - 如果提交时的输入值不为空,如何删除警报栏通知?
- symfony - 在 Symfony 3.4 中作曲家需要奏鸣曲项目/核心捆绑包
- c# - 静态上下文设置后为空
- angular - 属性中的角度订阅返回值
- r - cmd 中的 rmarkdown::render() 返回 pandoc 错误
- oracle-apex - 通过api创建新记录时,在apex oracle中自动打开模式窗口
- graphql - 如何使用 SitePage 查询填充搜索索引?
- python - 我们可以用 MIPCL “热启动” MILP 优化和初始可行解决方案吗?
- video.js - Videojs PIP 选项在 IE 和 Edge 中不起作用