Azure App Service - write to filesystem - ASP.NET web application

天涯浪子 提交于 2019-12-11 12:09:13

问题


I have an ASP.NET web application, created in Visual Studio 2017.

In the application there is a form, where the users can upload files. When files are uploaded, I process them and I save them on the filesystem:

var photo = Request.Files["file"];
photo.SaveAs("D:\\home");

Everything works perfect locally, but it's not working when I deploy the App on Azure App Services.

I read that, on Azure, the path

D:\home

should be writable.

But when I try the code I get this error:

Access to the path 'd:\home\test_image.JPG' is denied.

回答1:


Use %TEMP%, which on Azure App Service (Windows) resolves to d:\local. That's the local machine disk, not network storage, so significantly faster. It gets wiped on app restart so code accordingly.

var tempDirectoryPath = Environment.GetEnvironmentVariable("TEMP");
var filePath = Path.Combine(tempDirectoryPath, Request.Files["file"]);

If you want durability use Blob Storage instead.




回答2:


You should never use absolute paths on server components. Try something like this for instance

var folder = Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData);
var filePath = Path.Combine(folder, Request.Files["file"]);


来源:https://stackoverflow.com/questions/48947739/azure-app-service-write-to-filesystem-asp-net-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!