0

I've been trying to open an excel file in my ASP.NET MVC project (I use Syncfusion library for that). For some reason, when I publish the webapp with Visual Studio, I get the

"Access to the path 'D:\home\site\wwwroot' is denied."

error.

The file is correctly uploaded to the webserver (I can download it just fine when I input the path in the browser). I've looked into this question, but there is no WEBSITE_RUN_FROM_PACKAGE setting in my azure webapp. I went into Kudu, opened up the console and ran

icacls "D:\home\site\wwwroot"

which gave this output

D:\home\site\wwwroot Everyone:(I)(OI)(CI)(M,DC)
BUILTIN\Administrators:(I)(OI)(CI)(F)

So, with my little understanding of acls, it seems that Everyone has Modify permission to this folder.

Which means that I'm at a loss as to what to do.

Code:

IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;

application.EnablePartialTrustCode = true;

IWorkbook workbook = application.Workbooks.Create(1);
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
dir = dir.Replace("file:\\", "");
dir = dir.Replace("\\bin", "");

var dirXlsx = "\\Areas\\Stampe\\Content\\Excel\\RapportoProvaMare(version1).xlsx";

FileStream s = File.OpenRead(dir + dirXlsx);
                
IWorkbook source = excelEngine.Excel.Workbooks.Open(s);

I don't think the Syncfusion code has anything to do with the issue, I'm just reporting it for completeness.

Anybody have an idea as to what the issue could be?

EDIT: To be more specific, I'm publishing the code via Visual Studio, with the excel file in a folder of the website (just like you would do for an image). All the test I've made to the dir variable have been tested on the Azure environment (we have a duplicate test environment). I just need to read the excel file from the local website folder and process it before sending it to the user.

9Snick4
  • 31
  • 7
  • check this post out https://learn.microsoft.com/en-us/answers/questions/257169/azure-app-service-not-allowing-to-edit-the-files-p.html In general it is not advisable to store any files locally and process them in azure app service. you can store files in azure blob or file storage and use them. – Aravind Mar 26 '21 at 15:55
  • @Aravind thank you for your comment. I don't need to edit the file itself, because after the process I send it to download for the user. Does that still mean I would want to use blobs? – 9Snick4 Mar 26 '21 at 16:24
  • The thing is in general app service storage is ephemeral meaning the content will be lost if the app recycles or restarts. but if you use app service environment you do get dedicated storage. so it is cheaper and easier to use blobs or files for storage where the files will be persisted. – Aravind Mar 26 '21 at 16:27
  • @Aravind even if the file is part of the Visual Studio project that we regularly deploy to the Azure web app? – 9Snick4 Mar 29 '21 at 06:54

1 Answers1

1

Please use %HOME% for persistent file storage or %TMP% for temporal storage You can create a %HOME% environment variable in your local computer for test purposes.

Juanma Feliu
  • 1,298
  • 4
  • 16
  • Can you share a code example? Do I need to map these variables or would just putting them into the path string work? – 9Snick4 Mar 29 '21 at 06:59
  • Just use them inside string. %HOME% maps to /home and files are stored in a STA blob storage. – Juanma Feliu Mar 29 '21 at 07:10
  • https://github.com/projectkudu/kudu/wiki/Understanding-the-Azure-App-Service-file-system – Juanma Feliu Mar 29 '21 at 07:14
  • I've tried to incorporate the %HOME% in the path and I keep getting a file not found error like this: ` Could not find a part of the path 'D:\%HOME%\site\wwwroot\Areas\Stampe\Content\Excel\RapportoProvaMare(version1).xlsx'. ` Seems like it's not resolving the path. I've tried to change the variable `dir` to `"%HOME%\\site\\wwwroot" and to "\\%HOME%\\site\\wwwroot" and both gave this error (both depicting a path with the variable not resolved). What could I be doing wrong? – 9Snick4 Mar 29 '21 at 09:45
  • var dir = "%HOME%" – Juanma Feliu Mar 29 '21 at 09:54
  • Tried `dir = "%HOME%"`. Still this: `Could not find a part of the path 'D:\Windows\system32\%HOME%\Areas\Stampe\Content\Excel\RapportoProvaMare(version1).xlsx'.` I'm connected to the FTP and the file exists. I really am at a loss here... – 9Snick4 Mar 29 '21 at 10:13
  • %HOME% is an environment variable for your Azure App Service, not for your local machine. Do you need to read the excel from a ftp on-premise? explain better what u doing. Are you uploading your excels to Azure? – Juanma Feliu Mar 29 '21 at 10:20