gpt4 book ai didi

c# - 读取文件时出现 UnauthorizedAccessException

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:26 25 4
gpt4 key购买 nike

我有 ASP Web API,我正在尝试从那里的调用返回一个文件。

在我的服务器上,我不断收到错误:

System.UnauthorizedAccessException: Access to the path 'E:\Data\Docs\specific\document.pdf' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at GIS.Backend.Assets.BLL.Document.GetByUrl(String url) at GIS.Backend.Assets.WebApi.Controllers.DocumentController.Get(String url)

我猜这是错误的地方:

string documentenPath = System.Web.Configuration.WebConfigurationManager.AppSettings["DocumentenDir"].ToString();
string fullUrl = documentenPath + url;

Stream file = new FileStream(fullUrl, FileMode.Open);
return file;

我已将 IIS_IUSRS 设置为具有对“文档”文件夹的读取权限,所以它应该能够正确读取?

最佳答案

使用 FileAccess.Read 标志打开流

FileStream fs = new FileStream(fullUrl, FileMode.Open, FileAccess.Read);

关于c# - 读取文件时出现 UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44112194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com