.Net Core 2 "DirectoryNotFoundException: Could not find a part of the path"



我正在尝试编写一个web应用程序,允许用户将文件上传到服务器,然后将其保存在网络共享(即X:\TargetFolder(上。首先是代码:

using (var fileStream = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
var inputStream = file.OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}

targetFile变量的内容是:

"X:\Intranet\IN\PartesTrabajo"

当这段代码在Kestrel或IIS Express中运行时,它可以正常工作,但当网站在IIS 7.5中运行时我会得到以下异常:

2017-08-30 14:51:05.742 -04:00 [Error] An unhandled exception has occurred while executing the request
System.IO.DirectoryNotFoundException: Could not find a part of the path 'X:IntranetINPartesTrabajoA1_7706_Copy of All Web Servers Batch 1.xlsx'.
at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Instrumentacion.Helpers.UtilityLibrary.<WriteFileInput>d__4.MoveNext() in C:UserskellyDocumentsSite RemediationRemediated SitesBarcelonaInstrumentacionHelpersFileHelpers.cs:line 42

这显然是一个与IIS相关的权限问题,但我不知道问题出在哪里。

该应用程序汇集了我的。在中运行的Net Core应用程序(名为.NetCore(被配置为以ApplicationPoolIdentity运行。我已向IIS AppPool添加了明确的完全控制权限。此X:驱动器共享的NetCore用户。我试图将应用程序池用户切换到网络服务(并为其设置明确的权限(,但遇到了同样的错误。我已经将此共享指向本地文件夹,但一直收到此错误,但是,如果我将保存指向实际文件夹(即C:\Users…\TargetFolder(,则写入操作将按预期进行。

我不知道还能去哪里看!

它适用于Kestrel和IIS Express,因为应用程序在用户会话的上下文中运行。

在用户会话中映射的网络驱动器只能由该用户访问,而不能由运行IIS的任何服务帐户访问。

如果您改为使用UNC路径,您将看到这是有效的。

相关内容

最新更新