访问wwwroot-Asp.Net Core MVC在本地主机上运行良好,但在已发布的应用程序中不起作用

问题描述 投票:0回答:1

我在尝试使我的应用正常运行时遇到很多麻烦发表。基本上,代码应该从中创建文档使用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();
}
asp.net-core-mvc openxml-sdk wwwroot
1个回答
0
投票

您的.SaveAs()字段应该是相对的,当前它实际上是保存到驱动器上某个位置的wwwroot。您可以通过几种不同的方式指定相对路径-下面是其中一种:

var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");
© www.soinside.com 2019 - 2024. All rights reserved.