Azure 应用服务 - 写入文件系统 - ASP.NET Web 应用程序

Gui*_*acy 5 asp.net file-io azure azure-web-app-service

我有一个在 Visual Studio 2017 中创建的 ASP.NET Web 应用程序。

在应用程序中有一个表单,用户可以在其中上传文件。上传文件后,我会处理它们并将它们保存在文件系统中:

var photo = Request.Files["file"];
photo.SaveAs("D:\\home");
Run Code Online (Sandbox Code Playgroud)

一切都在本地完美运行,但是当我在 Azure 应用服务上部署应用程序时它不起作用。

我读到,在 Azure 上,路径

D:\home
Run Code Online (Sandbox Code Playgroud)

应该是可写的。

但是当我尝试代码时,我收到此错误:

Access to the path 'd:\home\test_image.JPG' is denied.
Run Code Online (Sandbox Code Playgroud)

evi*_*obu 7

使用%TEMP%,在 Azure 应用服务 (Windows) 上解析为d:\local. 那是本地机器磁盘,而不是网络存储,因此速度要快得多。它在应用程序重新启动时被擦除,因此相应地编码。

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

如果您想要持久性,请改用 Blob 存储。


Ale*_*idt 6

您永远不应该在服务器组件上使用绝对路径。例如尝试这样的事情

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