Search code examples
asp.net-core-mvcopenxml-sdkwwwroot

Access to wwwroot - Asp.Net Core MVC working well on local host but not in published app


I'm having a lot of trouble trying to get my App to work when published. Basically, the code is supposed to create a doc from template using Open XML sdk, then save to wwwroot and then upload to blob storage.

It's working fine using local host. Have read and tried some stuff re accessing static files - but nothing seems to work. Any help would be very much appreciated. Relevant code is below:

[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();
}

Solution

  • Your .SaveAs() field should be relative, currently its literally saving to wwwroot somewhere on the drive. You can specify the relative path a few different ways - one of them is below:

    var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");